ChangeNotification in thread

Discussion of open issues, suggestions and bugs regarding SDAC (SQL Server Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
kme
Posts: 5
Joined: Wed 21 Oct 2009 13:00

ChangeNotification in thread

Post by kme » Sat 13 Feb 2010 08:50

I have a problem with the ChangeNotification component in a thread. I works ok except that the event OnChange is not run. I only get the event when I finish the thread. If I put in Application->ProcessMessages in the execute loop section it work ok and events are run, but I think running ProcessMessages is not an good idea in a thread.

So to me it looks like I must run some message handling. I guess it's not an ChangeNotification issue, but more of a general thread issue.

I use other components (TCP connection) and does not have the same event problems with these.

Dimon
Devart Team
Posts: 2885
Joined: Mon 05 Mar 2007 16:32

Post by Dimon » Mon 15 Feb 2010 09:00

I can't reproduce the problem.
Try to compose a small sample to demonstrate the problem and send it to dmitryg*devart*com.

Dimon
Devart Team
Posts: 2885
Joined: Mon 05 Mar 2007 16:32

Post by Dimon » Mon 15 Feb 2010 14:48

Thank you for the information.
The problem is that the TMSChangeNotification component calls the OnChange event handler in the same thread, that it is started. In your case, the ChangeNotificationChange method should be called in the TMessageServerThreadClass thread. Therefore, to solve the problem, you should call the Application->ProcessMessages() method in the TMessageServerThreadClass thread to process the OnChange event handler.

But not to use the separate thread for TMSChangeNotification and use only main thread is a better way.

kme
Posts: 5
Joined: Wed 21 Oct 2009 13:00

Post by kme » Tue 16 Feb 2010 15:09

I really want to have it in a separate thread because my main thread could be busy for a while.

I have tried to use events on the TMSQuery component in the same thread (eg OnNewRecord), and they work ok without ProcessMessages(). Why does TMSChangeNotification need the ProcessMessages() to run?

kme
Posts: 5
Joined: Wed 21 Oct 2009 13:00

Post by kme » Tue 16 Feb 2010 20:42

Update: I found some examples to handle messages in a thread, and I have tried this code with success:

Code: Select all

MSG msg;
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
	TranslateMessage(&msg);
	DispatchMessage(&msg);
}
According to other this should be a safe way to handle messages, instead of Application->ProcessMessages() which is likely to cause trouble (not thread safe ?).

Dimon
Devart Team
Posts: 2885
Joined: Mon 05 Mar 2007 16:32

Post by Dimon » Wed 17 Feb 2010 08:35

kme wrote:I really want to have it in a separate thread because my main thread could be busy for a while.

I have tried to use events on the TMSQuery component in the same thread (eg OnNewRecord), and they work ok without ProcessMessages(). Why does TMSChangeNotification need the ProcessMessages() to run?
Unlike events of TMSQuery, the TMSChangeNotification component uses separate thread to process data from SQL Server and call the OnChange event handler.

Dimon
Devart Team
Posts: 2885
Joined: Mon 05 Mar 2007 16:32

Post by Dimon » Wed 17 Feb 2010 08:35

kme wrote:Update: I found some examples to handle messages in a thread, and I have tried this code with success:

Code: Select all

MSG msg;
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
	TranslateMessage(&msg);
	DispatchMessage(&msg);
}
According to other this should be a safe way to handle messages, instead of Application->ProcessMessages() which is likely to cause trouble (not thread safe ?).
Yes, It is a correct way to handle messages.

Post Reply