views:

204

answers:

2

I made a small app that connects to a mysql db using dbx. It works ok with my local mysql server, but it's supposed to work with a remote server.

Connecting to the remote server takes a few seconds, which freezes the app.

So my question is, how can I put the connection code in a different thread?

I'll have to pass that connection to the main thread somehow, so that the dbgrid I have on the main form works.

I read that db stuff working in a different thread should have their own connections. So I'm not sure how to do what I want.

Any ideas? Anything to read about working with remote servers?

Thanks.

Edit: The components I'm using on the form are: TSQLConnection -> TSimpleDataSet > TDataSource > TDBGrid.

A: 

this has really helped me for doing multi thread Apps in rad studio Writing multi-threaded applications Index

if their is any thing else post and ill try to help

Jonathan D
I've been reading that section of the help, but I'm still not sure how to go about making a dbx connection in a separate thread and sharing it with the main thread.
Mario
+1  A: 

You only need a connection per thread if your threads are going to do simultaneous database access. Basically what you want, is for a thread to connect, and come back to you when the connection has been established. You can do this in a thread, and when the thread is ready (i.e. connection established), it can send a message back to the main thread to let it know that the dbx connection is now available. See this tutorial for ideas on how to set up the thread, and communicate between the thread and the main VCL thread. Threading Tutorial

Steve
+1 Yes, resources like network connections can get out of sync in multi-threaded apps. Same applies to FTP connections, file pointers, etc. So creating a separate resource per thread is the way to go.
Bill Karwin