Page 1 of 1

ChangeNotification in thread

Posted: Sat 13 Feb 2010 08:50
by kme
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.

Posted: Mon 15 Feb 2010 09:00
by Dimon
I can't reproduce the problem.
Try to compose a small sample to demonstrate the problem and send it to dmitryg*devart*com.

Posted: Mon 15 Feb 2010 14:48
by Dimon
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.

Posted: Tue 16 Feb 2010 15:09
by kme
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?

Posted: Tue 16 Feb 2010 20:42
by kme
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 ?).

Posted: Wed 17 Feb 2010 08:35
by Dimon
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.

Posted: Wed 17 Feb 2010 08:35
by Dimon
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.