CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Jun 2002
    Posts
    24

    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.

  2. #2
    Join Date
    Sep 2001
    Location
    Québec, Canada
    Posts
    1,923
    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
    CodeGuru VB FAQ Visual Basic Frequently Asked Questions
    VB Code color Tool to color your VB code on CodeGuru
    Before you post Importants informations to know before posting

  3. #3
    Join Date
    Sep 2000
    Posts
    77
    GetExitCodeThread should solve your problem.

    It returns STILL_ACTIVE if the thread is alive.

    - Kiran.

  4. #4
    Join Date
    Jun 2002
    Posts
    24
    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,

  5. #5
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868
    Try OpenThread.

  6. #6
    Join Date
    Sep 2001
    Location
    Québec, Canada
    Posts
    1,923
    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
    CodeGuru VB FAQ Visual Basic Frequently Asked Questions
    VB Code color Tool to color your VB code on CodeGuru
    Before you post Importants informations to know before posting

  7. #7
    Join Date
    Jun 2002
    Posts
    24
    are you talking about openthreadtoken?? that also requires HANDLE...is there any other way to use that API??

    thanks,

  8. #8
    Join Date
    Sep 2001
    Location
    Québec, Canada
    Posts
    1,923
    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
    CodeGuru VB FAQ Visual Basic Frequently Asked Questions
    VB Code color Tool to color your VB code on CodeGuru
    Before you post Importants informations to know before posting

  9. #9
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868
    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

  10. #10
    Join Date
    Jun 2002
    Posts
    24
    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

  11. #11
    Join Date
    Jun 2002
    Posts
    24
    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
  •  





Click Here to Expand Forum to Full Width

Featured