Question: I have a thread that runs in a dll that I dynamic link with in my main app. Is there a way to wait for all threads in an .exe (including it's loaded dll's) without knowing the thread handle? Windows 7 x64, vc++
The thread is a function that does some processing on a certain file, it is not expected to return anything, it works upon a global class that is modified in certain stages of the thread completion. The thread function calls upon other functions .
I want to wait until the last line of the function is executed.

Answer: 
I never did this myself, but you could probably

1. create a snapshot using [`CreateToolhelp32Snapshot`](http://msdn.microsoft.com/en-us/library/windows/desktop/ms682489%28v=vs.85%29.aspx)
2. then enumerate the threads using [`Thread32First`](http://msdn.microsoft.com/en-us/library/windows/desktop/ms686728%28v=vs.85%29.aspx) and [`Thread32Next`](http://msdn.microsoft.com/en-us/library/windows/desktop/ms686731%28v=vs.85%29.aspx)
3. for each thread ID, use [`OpenThread`](http://msdn.microsoft.com/en-us/library/windows/desktop/ms684335%28v=vs.85%29.aspx) to aquire a handle. Make sure that you open the thread with the [`SYNCHRONIZE`](http://msdn.microsoft.com/en-us/library/windows/desktop/aa379607%28v=vs.85%29.aspx) privilege so that you can, at last
4. pass all thread handles to [`WaitForMultipleObjects`](http://msdn.microsoft.com/en-us/library/windows/desktop/ms687025%28v=vs.85%29.aspx) to wait for all of them to terminate.
Another solution, assuming that all you want is to stop the main() function from running but not exit the process until any other threads are complete, is to call

```
ExitThread(GetCurrentThread());

```

from within main(). If you don't call `ExitProcess`, either explicitly or by returning from main(), Windows will not exit until the last thread exits.

Note that there is a major problem with doing this, no matter how you approach it: if one of the Windows APIs you use has launched a thread that isn't going to exit, your application won't exit either.

The proper solution is for the DLL itself to contain a shutdown function that waits for its own threads to exit if necessary.