Update TForm Function From TThread

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

Update TForm Function From TThread

Postby rajesh » Fri Jul 22, 2016 7:06 am

I have Function UpdateDatabase(String , String , int , int) that update database in TForm by passing some parameters using TQuery.
Now I have 100 of threads that update database by calling the TForm Function.
Like from thread
Form1->UpdateDatabase(xyz,abc,1,2) ;
But it seems Form1 Hang frequenty . Can you suggest how call TForm Function called in Synchronized way form thread
so that TForm Not Hang
rajesh
Top Poster
Top Poster
 
Posts: 43
Joined: Mon Oct 26, 2009 1:25 am

Re: Update TForm Function From TThread

Postby rlebeau » Fri Jul 22, 2016 3:18 pm

rajesh wrote:Now I have 100 of threads that update database by calling the TForm Function.
Like from thread
Form1->UpdateDatabase(xyz,abc,1,2) ;


That is not thread-safe. You need to synchronize the calls with the main UI thread.

rajesh wrote:Can you suggest how call TForm Function called in Synchronized way form thread so that TForm Not Hang


TThread has a Synchronize() method:

Code: Select all
void __fastcall TMyThread::UpdateDatabase()
{
    Form1->UpdateDatabase(xyz, abc, 1, 2);
}
...
Synchronize(&UpdateDatabase);


Alternatively, give each thread its own database connection (or use a connection pool) and TQuery object, and then perform the update query directly in your thread without having to involve the Form at all.
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1395
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA


Return to Technical

Who is online

Users browsing this forum: No registered users and 6 guests

cron