Obtain thread handles/id of a specific process

352 Views Asked by At

I have a multi-threaded embedded architecture that contains 6 application specific processes which are executed when the initialization process is executed. Likewise, each have their own number of threads that are running.

What i want to do is suspend the running threads of 1 particular process based on whether the device is connected to the pc or not.

I have tried searching around and the closest i've found to what im looking for is the following: How to obtain list of thread handles from a win32 process?

However, that code returns the list of all running threads. This wont work for me since im trying to suspend all obtained threads, assuming they have been obtained from the same process, thus i do not check which process they belong too.

Likewise, i am obtaining the list of running threads of a processes in another process.

Is there an existing method from windows that allows such control, or am i stuck with having to identify which threads i need to suspend from the entire list?

1

There are 1 best solutions below

0
On BEST ANSWER

Instead of trying to forcefully suspend threads (which is likely to bring you trouble when you suspend in "not so lucky moment") you'd rather use a named CreateEvent() with manual reset.

  • Named events are easily shared between processes. You simply CreateEvent() again with the same name. The typical name for event would be MyCompany_MyProduct_MyFeature_EventName to prevent accidental collisions.
  • When you WaitForSingleObject() on "set" event, the wait is immediately satisfied.
  • When you wait on "reset" event, the wait suspends your thread until event is set.
  • Your first application will have its thread(s) wait on event when they're not doing any work and therefore safe to suspend.
  • You will set and reset event from second application to control the first application.
  • This way, you don't need to enumerate threads, and it's more robust.