|
-
Oct 24th, 2001, 12:03 AM
#1
Thread Starter
Registered User
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
-
Oct 24th, 2001, 12:43 AM
#2
Thread Starter
Registered User
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:
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
-
Oct 24th, 2001, 12:54 AM
#3
PowerPoster
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]
'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
-
Oct 24th, 2001, 01:00 AM
#4
Thread Starter
Registered User
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?
-
Oct 24th, 2001, 01:44 AM
#5
PowerPoster
You are not getting the ExitCode right
VB Code:
GetExitCodeThread(TID, lpExitCode)
It's failing for some reason.
-
Oct 24th, 2001, 01:57 AM
#6
Thread Starter
Registered User
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
-
Oct 24th, 2001, 03:54 AM
#7
PowerPoster
But you still need the ExitCode
-
Oct 24th, 2001, 06:13 PM
#8
Thread Starter
Registered User
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|