Suspend Threads

  • Thread starter Thread starter elziko
  • Start date Start date
E

elziko

This is a repost from microsoft.public.dotnet.languages.vb but I had no luck
there so maybe this is a better place to ask?

I'd like to 'pause' a process that has nothing to do with my application. To
do this I intend to suspend every thread in the process.

So far I have managed to get the System.Diagnostics.Process and all the
ProcessThreads running under that. My first question is, what's the
relationship between a ProcessThread and a Thread? From what I have read for
every real thread there is a managed thread. Does this mean their IDs are
the same?

I have then used the OpenThread API to get the handle for the thread using
the ID from each ProcessThread. However the handle that OpenThread seems to
return is the same for each ProcessThread. Infact the return value is always
8589934592 which incidently is the number of bits in a gigabyte(!).

The next plan is to use the SuspendThread API to suspend each of these
threads which I can't do until I'm sure I'm getting the correct Thread
handles.

Does any one have any comments or advice (especially on keeping as much of
the code as possible managed) on how to get this working?

Here is my current code in case that helps:

Declare Function OpenThread Lib "kernel32" (ByVal dwDesiredAccess As Long,
ByVal bInheritHandle As Boolean, ByVal dwThreadId As Long) As Long

and...

Dim AllProcesses As Process() =
System.Diagnostics.Process.GetProcessesByName("MyProcess")
Dim MyProcess As Process = AllProcesses(0)
For Each ProcessThread As ProcessThread In ActiveSyncProcess.Threads
Console.WriteLine(OpenThread(1, True, ProcessThread.Id))
Next
 
Why don't you then d
For Each ProcessThread As ProcessThread In ActiveSyncProcess.Thread
ProcessThread.Suspend(

For unsigned ints all bits set would be a max int value. Your OpenThread probably returns -1 signed int (which is yes all bits set) 0xFFFFFFFF. This indicates an error

To your question about processes: each process has at least one thread. Their IDs are not related, at least I would not count on it.
 
Why don't you then do
For Each ProcessThread As ProcessThread In ActiveSyncProcess.Threads
ProcessThread.Suspend()

Because the ProcessThread class doesnt have a method called Suspend! The
Suspend method is a member of the Thread class hence my need to create a
Thread from a ProcessThread.
To your question about processes: each process has at least one thread.
Their IDs are not related, at least I would not count on it.

I understand the relationship between Processes and Threads but what I need
to understand is the relationship between a ProcessThread and a Thread.

Thanks for replying though!
 
get a collection of all the ProcessThread objects associated with the
current process

Yup, I can do that.
get the Threads property of the Process instance, and then call Suspend on
each thread?

This is what I was initially trying to do but I can't do this since
'Threads' is actually a collection of System.Diagnostics.ProcessThreads
which unlike
System.Threading.Threads don't have a suspend method.
 
Back
Top