I am creating a WPF managed interface for an outdated Win32 application. The WPF interface is executable; as part of its startup routines, I run the legacy application as a DLL in the second thread. Any UI operation (including CreateWindowsEx , etc.) by the outdated application is returned to the main user interface thread.
As part of the application shutdown process, I want to clean it properly. Among other things, I want to call DestroyWindow in all unmanaged windows so that they can properly clean themselves. Thus, during shutdown, I use EnumWindows to try to find all of my unmanaged windows. Then I call DestroyWindow one of the list that I generate. They run on the main UI thread.
After this background knowledge, to my question:
In the EnumWindows enumeration procedure, I have to check if one of the returned top-level windows is one of my unmanaged windows. I do this by calling GetWindowThreadProcessId to get the process id and thread id of the window creator. I can compare the process id with Process.GetCurrentProcess().Id to check if my application is created.
For added security, I also want to see if my main user interface has created a window. However, the returned thread identifier is OS ThreadId (which is different from the managed thread identifier). As explained in this question , the CLR reserves the right to redistribute the managed thread to different OS threads. Can I rely on the CLR to be βsmart enoughβ to never do this for the main user interface thread (due to the proximity of the thread to the user interface)? Then I could call GetCurrentThreadId to get the main thread identifier of the UI thread for comparison.
Daniel Rose May 7 '10 at 7:46 a.m. 2010-05-07 07:46
source share