Results 1 to 8 of 8

Thread: Terminate Thread

  1. #1

    Thread Starter
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530

    Terminate Thread

    Anyone have some working code that terminates a thread using TerminateThread win32api?

    To work I think it needs openThread api too. I am after some code that uses both api functions in VB to terminate a thread

  2. #2

    Thread Starter
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530

    To see what I mean

    I am trying to close an async mesagebox started with the win32 api call (therefore in its own thread within my app's process).

    Here is the code I have so far:

    To see what I mean, I am trying to close a message box started with the message box api, while leaving the application alone:

    VB Code:
    1. Option Explicit
    2. Private Declare Function MessageBoxEx Lib "user32" Alias "MessageBoxExA" (ByVal hwnd As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal uType As Long, ByVal wLanguageId As Long) As Long
    3.  
    4. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    5. Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
    6. Private Declare Function TerminateThread Lib "kernel32" (ByVal hThread As Long, ByVal dwExitCode As Long) As Long
    7. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    8. Private Declare Function GetExitCodeThread Lib "kernel32" (ByVal hThread As Long, lpExitCode As Long) As Long
    9. Private Declare Sub ExitThread Lib "kernel32" (ByVal dwExitCode As Long)
    10.  
    11.  
    12. Private Sub Command1_Click()
    13. 'display asynch message box
    14.     MessageBoxEx 0&, "Pulchritude" & vbNewLine & "Ansync Messagebox", "Discombobulated", 0&, 0
    15. End Sub
    16.  
    17.  
    18. Private Sub Command2_Click()
    19. 'attempt to close asynch message box
    20. Dim hwnd                As Long
    21. Dim Pid                 As Long
    22. Dim TID                 As Long
    23. Dim lpExitCode          As Long
    24. Dim hThread             As Long
    25.  
    26. hwnd = FindWindow("#32770", "Discombobulated")
    27.  
    28. If hwnd Then
    29.    
    30.     TID = GetWindowThreadProcessId(hwnd, Pid)
    31.     Call GetExitCodeThread(TID, lpExitCode)
    32.     'fine to here but now what?
    33.  
    34. End If
    35. End Sub

  3. #3
    PowerPoster
    Join Date
    Aug 2000
    Location
    India
    Posts
    2,288
    This is an example from allapi.net
    VB Code:
    1. 'In a form
    2. 'Add a command button to the form
    3. Private Sub Command1_Click()
    4.     'KPD-Team 1999
    5.     'URL: [url]http://www.allapi.net/[/url]
    6.     'E-Mail: [email][email protected][/email]
    7.     'After you click this button, try to move the window
    8.     'You will see that the AsyncThread-function was executed asynchronously
    9.     hThread = CreateThread(ByVal 0&, ByVal 0&, AddressOf AsyncThread, ByVal 0&, ByVal 0&, hThreadID)
    10.     CloseHandle hThread
    11. End Sub
    12. Private Sub Form_Unload(Cancel As Integer)
    13.     'If the thread is still running, close it
    14.     If hThread <> 0 Then TerminateThread hThread, 0
    15. End Sub
    16. 'In a module
    17. Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    18. Declare Function CreateThread Lib "kernel32" (lpThreadAttributes As Any, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, lpParameter As Any, ByVal dwCreationFlags As Long, lpThreadID As Long) As Long
    19. Declare Function TerminateThread Lib "kernel32" (ByVal hThread As Long, ByVal dwExitCode As Long) As Long
    20. Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    21. Public hThread As Long, hThreadID As Long
    22. Public Sub AsyncThread()
    23.     'Let this thread sleep for 10 seconds
    24.     Sleep 10000
    25.     hThread = 0
    26. End Sub

  4. #4

    Thread Starter
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    amitabh,

    Thanks for that example, however, it doesn't use openthread api so it doesn't help me with my predicament as the thread already exists. You haven't come across a vb declare of the openthread api have you?

  5. #5
    PowerPoster
    Join Date
    Aug 2000
    Location
    India
    Posts
    2,288
    You are not getting the ExitCode right
    VB Code:
    1. GetExitCodeThread(TID, lpExitCode)
    It's failing for some reason.

  6. #6

    Thread Starter
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    Actuallly I left that in there from my attempt to use exitthread api, should be removed as I don't think it is needed if using terminatethread

  7. #7
    PowerPoster
    Join Date
    Aug 2000
    Location
    India
    Posts
    2,288
    But you still need the ExitCode

  8. #8

    Thread Starter
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    K thanks. Any ideas on how to terminate a pre-existing thread?

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