SciBit

Forums
It is currently Sat Nov 01, 2014 1:01 pm

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Tue Dec 13, 2005 7:30 am 
Site Admin

Joined: Wed May 21, 2003 7:41 am
Posts: 951
Definitely an issue that arises more often than not in real world applications, and definitely one that pops up on our forum from time to time.

Firstly, a bit of the fundamentals. MyComponents are derived from the standard Borland TCustomConnection and TDataset classes, and it is therefore important to realize that if these base classes' methods are not thread protected (using synchronization), neither are those of the MyComponents. That said, the MyComponents are quite safe to use in an isolated manner from and within threads (TThread). Moreover, the MyComponents classes actually makes special provision for you to create threaded connections to the MySQL server.

Also to be kept in mind, is that all Borlands' GUI VCL runs in the main application thread, and therefore is it not possible to create a dataset in a thread and have it be connected to a DBGrid for example. However, the TMySQLDataset does have a thread open command called Execute, which will effectively open the dataset in a different thread, and then only enable itself for GUI VCL access, see your MyComponents documentation on this method and more.

So, what does all this mean in layman's terms? Well, in short, if your thread "owns" the TMySQLDataset/Query, then should you have no problem using it in a normal way from the thread. You can do this in a number of ways, ex:
* A synchronized "pool" of datasets from which a TThread might "borrow" a dataset in an organized manner (you can even use the TMySQLServer's flying datasets for this, see articles on this forum)
* Another way is to create the actual dataset in the thread itself, i.e. as a local thread variable.

There are only two rules for using a dataset in a thread:
1. You absolutely must set the ShareConnection property to False, so that it will create itself a new MySQL connection and not share a connection with other thread and non-threaded datasets or the TMySQLServer
2. You absolutely have to open it only in the TThread.Execute method in order for it to open a MySQL connection only at that time using the thread's ThreadID and ProcessID to do so.

If you do this, will you have only joy with MyComponents and threads. A lot more information is available on this forum (simply do a search for "thread") as well as various TMySQLDataset properties in your MyComponents manual, ex ShareConnection, Execute method, Options property


Top
 Profile  
 
 Post subject: Simple Example
PostPosted: Thu Feb 14, 2008 5:36 am 
Advanced

Joined: Wed Feb 13, 2008 4:38 am
Posts: 12
is there simple example to implementation MyComponent (MysqlQuery) with TThread


Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 14, 2008 6:07 am 
Site Admin

Joined: Wed May 21, 2003 7:41 am
Posts: 951
Well, unfortunately there is nothing simple about using threads with any situation, nevermind MyComponents.

For this reason we have made it very simple from your perspective to use threads. First, the threaded update:

All you have to do is enable the Options.doThreadedUpdate option of any specific dataset. All updates/deletes/inserts of that dataset will now happen via a thread, i.e. your main application will respond even while an update is in progress. Also see the OnThreadPosted event in the helpfile.

Secondly, is the threaded open/select:

All you have to do is instead of opening your dataset in the normal way, i.e. using Open or Active=True, is to open it using the Dataset.Execute method. Again, your main application will now keep on functioning while the select query is executed in the background. Also see the OnThreadOpened event in the helpfile.

All dataset components, e.g. TMySQLDataset/Query/Table, have this functionality.

If you want to implement your own threads and use datasets within them, then the article at the top is the perfect starting/reference point. The MySQLThreads.pas contains the MyComponents code to use datasets with threads in various ways, i.e. it actually creates threads and uses datasets in the correct manner in those threads.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group