|
-
August 23rd, 2002, 06:06 PM
#1
how to know if thread is live?
I have a pool of thread ids, I wanna know if a thread is alive? How would I get to know that a thread id "xyz" is live or has destroyed?? Any API calls?? any idea??
khushia.
-
August 23rd, 2002, 06:25 PM
#2
The API ResumeThread() will give an error if the thread cannot restart, so that way you'll be able to know if the thread is froze or not, I did not test it but it should work. The problem is that the parameters to pass is a Thread Handle and not a thread ID, is that a problem in your case?
API WebSites:
http://www.allapi.net
http://www.vbapi.com
JeffB - hope it helps
-
August 24th, 2002, 01:13 AM
#3
GetExitCodeThread should solve your problem.
It returns STILL_ACTIVE if the thread is alive.
- Kiran.
-
August 26th, 2002, 02:05 PM
#4
Kiran and JeffB, thanks but problem is, I have thread id and could not find any api that could give me HANDLE from thread id. Your help in getting HANDLE from id would be highly appreciated.
Thanks,
-
August 26th, 2002, 02:20 PM
#5
-
August 26th, 2002, 02:54 PM
#6
OpenThread() API do not exits in all Windows version
There is no way to get the thread handle from the thread ID. While there is an OpenProcess() API that takes a PID and returns the handle to the process, there is no corresponding OpenThread() that takes a thread ID and returns a thread handle. - Copyright Microsoft MSDN
KB Number: Q127992
http://support.microsoft.com/default...;en-us;Q127992
JeffB
-
August 26th, 2002, 02:56 PM
#7
are you talking about openthreadtoken?? that also requires HANDLE...is there any other way to use that API??
thanks,
-
August 26th, 2002, 03:13 PM
#8
No, OpenThread is a different api than OpenThreadToken, OpenThread is provided on Windows XP, Windows 200 and ME, but not on win98 and NT4, you cannot use OpenThread. I think you'll have to use DebugActiveProcess or DuplicateHandle, the link on MSDN is all of what I know about that...
Since you use thread, if you use CreateThread, that function return the Handle of the thread, without the handle, you can't do anything with a thread for security purpose (form MSDN...)
JeffB
-
August 26th, 2002, 03:39 PM
#9
According to docs this should work on 95 & 98... give it a shot:
Option Explicit
Const TH32CS_SNAPHEAPLIST = &H1
Const TH32CS_SNAPPROCESS = &H2
Const TH32CS_SNAPTHREAD = &H4
Const TH32CS_SNAPMODULE = &H8
Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE)
Const TH32CS_INHERIT = &H80000000
Const MAX_PATH As Integer = 260
Private Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String * MAX_PATH
End Type
Private Type THREADENTRY32
dwSize As Long
cntUsage As Long
th32ThreadID As Long
th32OwnerProcessID As Long
tpBasePri As Long
tpDeltaPri As Long
dwFlags As Long
End Type
Private Declare Function Thread32First Lib "KERNEL32.DLL" (ByVal hSnapshot As Long, ByRef lpte As THREADENTRY32) As Long
Private Declare Function Thread32Next Lib "KERNEL32.DLL" (ByVal hSnapshot As Long, ByRef lpte As THREADENTRY32) As Long
Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Private Sub Command1_Click()
'Find the owner process to thread 1000
FindThreadProcess 1000
End Sub
Private Sub FindThreadProcess(lThreadParam As Long)
Dim hSnapshot As Long
Dim uProcess As PROCESSENTRY32
Dim uThread As THREADENTRY32
Dim r As Boolean
Dim strProcName As String
Dim strEXE As String
Dim lProcID As Long
Dim lThreadID As Long
'Takes a snapshot of the processes and the heaps, modules, and threads used by the processes
hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0&)
uThread.dwSize = Len(uThread)
r = Thread32First(hSnapshot, uThread)
Do While r
lThreadID = uThread.th32ThreadID
If lThreadID = lThreadParam Then
lProcID = uThread.th32OwnerProcessID
End If
r = Thread32Next(hSnapshot, uThread)
Loop
'set the length of our ProcessEntry-type
uProcess.dwSize = Len(uProcess)
'Retrieve information about the first process encountered in our system snapshot
r = Process32First(hSnapshot, uProcess)
Do While r
If lProcID = uProcess.th32ProcessID Then
strProcName = UCase(Left$(uProcess.szExeFile, IIf(InStr(1, uProcess.szExeFile, Chr$(0)) > 0, InStr(1, uProcess.szExeFile, Chr$(0)) - 1, 0)))
MsgBox "Thread " & Trim(CStr(lThreadParam)) & " belongs to " & strProcName
Exit Do
End If
'Retrieve information about the next process recorded in our system snapshot
r = Process32Next(hSnapshot, uProcess)
Loop
'close our snapshot handle
CloseHandle hSnapshot
End Sub
-
August 26th, 2002, 03:42 PM
#10
Thanks for giving the pointers...what a bad luck, i cant even browse openthread api page at msdn online, can somebody send me the text for API reference...
Thanks
-
August 26th, 2002, 04:37 PM
#11
Thanks DSJ, it worked fine. and thanks everyone who contributed.
Khushia.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|