Click here to Skip to main content
15,902,842 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Mine is a dynamic library, It creates a thread, For desktop root node called IUIAutomation::AddStructureChangedEventHandler registered a structure change event handler, the thread has been in the running state. When clicking to exit the program, it will affect the program to call TerminateProcess and ExitProcess normally, and the window of the program is Destroyed (WM_DESTROY), but the background can not be exited. Have you ever encountered this?
Thank you.

What I have tried:

Monitor TerminateProcess, ExitProcess, not called normally;
I monitor the WM_Destroy main window, and then call RemoveStructureChangedEventHandler to remove this event, then stop the UIA threads, but have no effect;
Updated 26-Dec-23 22:04pm
Richard MacCutchan 27-Dec-23 3:50am    
You need to send the same message(s) to the background process.
helloboboyu 27-Dec-23 4:00am    
I'm not sure what that means. Could you tell me more about it, please
Richard MacCutchan 27-Dec-23 6:35am    
I am going by your statement, "but the background can not be exited". But since we have no idea what your code is doing it is impossible to offer any further suggestions. Please use the Improve question link above, and add complete details of what is not working.
0x01AA 27-Dec-23 7:31am    
The background thread needs to be cooperative and check something like 'Thread::Terminate' and exit the tread's loop if requested.
helloboboyu 9-Jan-24 1:46am    
Because I need to keep monitoring these events until the process exits, my approach is to monitor the TerminateProcess function. My worker thread will not receive the termination event until the program calls this API to end itself, and then release all resources. . But the current problem is that it seems to prevent the program from receiving TerminateProcess before I release the resources. Check the main thread through windbg. It is calling KERNELBASE!UnregisterWaitEx to wait for an event. I am still analyzing what it is waiting for.

1 solution

No, I have not encountered that because I always design things so that does not happen. In a multi-threaded design I have found start up and shut down to be the two things that often take the most work. In my subscriber pattern framework, I designate Event 0 to alwayts be the termination event. In it, all threads call WaitForMultipleObjects and perform an action when one of the events they have subscribed to is signalled. When they receive the terminate event they release all resources they have obtained and then shut down. In other applications I pass a reference to a flag that is set when the application is terminating to every thread. Sometimes I have a structure of application-wide data which includes a termination flag and a reference to it is passed to every thread when they are created.

The point is there are many ways you can communicate with a thread to tell it when to terminate. Among them are event objects and shared variables. It is up to you to decide which is most appropriate for your application.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900