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 :cool:
Printable View
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 :cool:
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:
Option Explicit 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 Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long Private Declare Function TerminateThread Lib "kernel32" (ByVal hThread As Long, ByVal dwExitCode As Long) As Long Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long Private Declare Function GetExitCodeThread Lib "kernel32" (ByVal hThread As Long, lpExitCode As Long) As Long Private Declare Sub ExitThread Lib "kernel32" (ByVal dwExitCode As Long) Private Sub Command1_Click() 'display asynch message box MessageBoxEx 0&, "Pulchritude" & vbNewLine & "Ansync Messagebox", "Discombobulated", 0&, 0 End Sub Private Sub Command2_Click() 'attempt to close asynch message box Dim hwnd As Long Dim Pid As Long Dim TID As Long Dim lpExitCode As Long Dim hThread As Long hwnd = FindWindow("#32770", "Discombobulated") If hwnd Then TID = GetWindowThreadProcessId(hwnd, Pid) Call GetExitCodeThread(TID, lpExitCode) 'fine to here but now what? End If End Sub
This is an example from allapi.net
VB Code:
'In a form 'Add a command button to the form Private Sub Command1_Click() 'KPD-Team 1999 'URL: [url]http://www.allapi.net/[/url] 'E-Mail: [email][email protected][/email] 'After you click this button, try to move the window 'You will see that the AsyncThread-function was executed asynchronously hThread = CreateThread(ByVal 0&, ByVal 0&, AddressOf AsyncThread, ByVal 0&, ByVal 0&, hThreadID) CloseHandle hThread End Sub Private Sub Form_Unload(Cancel As Integer) 'If the thread is still running, close it If hThread <> 0 Then TerminateThread hThread, 0 End Sub 'In a module Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) 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 Declare Function TerminateThread Lib "kernel32" (ByVal hThread As Long, ByVal dwExitCode As Long) As Long Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long Public hThread As Long, hThreadID As Long Public Sub AsyncThread() 'Let this thread sleep for 10 seconds Sleep 10000 hThread = 0 End Sub
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?
You are not getting the ExitCode right
It's failing for some reason.VB Code:
GetExitCodeThread(TID, lpExitCode)
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 :)
But you still need the ExitCode
K thanks. Any ideas on how to terminate a pre-existing thread?