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.
ChangeNotification in thread
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.
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.
Update: I found some examples to handle messages in a thread, and I have tried this code with success:
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 ?).
Code: Select all
MSG msg;
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}Unlike events of TMSQuery, the TMSChangeNotification component uses separate thread to process data from SQL Server and call the OnChange event handler.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?
Yes, It is a correct way to handle messages.kme wrote:Update: I found some examples to handle messages in a thread, and I have tried this code with success:
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 ?).Code: Select all
MSG msg; while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); }