[RESOLVED] Pause Process Execution
I've seen some third party applications pause the execution of a process and then enable that process later.
As far as I can tell, there is no managed way to do this, so I've been looking at unmanaged ways via an API. I couldn't find any specific information on it, but I figured there must be something.
Does anyone know a method to complete my task or an API I could research?
Any information is appreciated.
Thanks
Re: Pause Process Execution
Perhaps you are looking for SuspendThread and ResumeThread APIs? The following is VB6.
Code:
Private Declare Function ResumeThread Lib "kernel32.dll" ( _
ByVal hThread As Long) As Long
Private Declare Function SuspendThread Lib "kernel32.dll" ( _
ByVal hThread As Long) As Long
Re: Pause Process Execution
Re: Pause Process Execution
Quote:
Originally Posted by
dee-u
Perhaps you are looking for SuspendThread and ResumeThread APIs? The following is VB6.
Code:
Private Declare Function ResumeThread Lib "kernel32.dll" ( _
ByVal hThread As Long) As Long
Private Declare Function SuspendThread Lib "kernel32.dll" ( _
ByVal hThread As Long) As Long
Thanks. I'll look into them :)
Re: Pause Process Execution
u can use sleep api
The Sleep function suspends the execution of the current thread for a specified interval.
Code:
Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
sleep 5000 ' sleep for 5 secs
Re: Pause Process Execution
Quote:
Originally Posted by
agent_007
u can use sleep api
The Sleep function suspends the execution of the current thread for a specified interval.
Code:
Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
sleep 5000 ' sleep for 5 secs
I don't want to pause the process for a set amount of time. I want it to be paused until the User decides to resume it.
I've been looking into the first two functions mentioned and it seems I need to get the Thread handle of the process and pass it as an argument to the function. The problem with that, is that I can't seem to figure out how to get the thread handle :P
I passed the Process Handle to the function, but that didn't work. So, I'm still working on finding the thread handle.
Re: Pause Process Execution
So I looked around Google a bit more and found a post in C#:
http://stackoverflow.com/questions/7...d-process-in-c
That code apparently worked for them, but I can't seem to get it to work for me. I translated the code and I would appreciate any help into figuring it out.
APIs and Flags:
vb Code:
Declare Function SuspendThread Lib "kernel32.dll" (ByVal hThread As IntPtr) As UInteger
Declare Function ResumeThread Lib "kernel32.dll" (ByVal hThread As IntPtr) As UInteger
Public Enum ThreadAccess As Integer
TERMINATE = 1
SUSPEND_RESUME = 2
GET_CONTEXT = 8
SET_CONTEXT = 16
SET_INFORMATION = 32
QUERY_INFORMATION = 64
SET_THREAD_TOKEN = 128
IMPERSONATE = 256
DIRECT_IMPERSONATION = 512
End Enum
Declare Function OpenThread Lib "kernel32.dll" (ByVal dwDesiredAccess As ThreadAccess, ByVal bInheritHandle As Boolean, ByVal dwThreadId As UInteger) As IntPtr
Suspend Thread Code:
vb Code:
Dim pid As Integer = Int32.Parse(lvwProcess.SelectedItems(0).SubItems(1).Text)
Dim p As Process = Process.GetProcessById(pid)
If lvwProcess.SelectedItems.Count = 0 Then
Else
If (p.ProcessName = String.Empty) Then
Else
For Each pThread As ProcessThread In p.Threads
Dim pOpenThread As IntPtr = OpenThread(ThreadAccess.SUSPEND_RESUME, False, CType(p.Id, UInteger))
If (pOpenThread = IntPtr.Zero) Then
Exit For
End If
SuspendThread(pOpenThread)
Next
End If
End If
Re: Pause Process Execution
It turns out I was passing the ID of the process to the API and not the ID of the thread. I have it fixed, though.
Thanks again :D