RAFAAJ
asked on
Threading !
What is the difference between "TerminateThread" and "ExitThread" APIs ?
I have created a new thread to process a lenghthy loop so VB doesn't hang and I can still get on with my other work during the loop.
I used the CreateThread API and it works as expected . What I CAN'T do is end the thread prematurely so I can exit the loop if it is far too long.
I have tried the 2 APIs above to kill the thread but it crashes the whole app ! (:
I have have already seen the example given on AllAPI.net Site and searched the net for other more info but I can't find anything that works to end a thread !!!
Does anybody have a working example with "TerminateThread" or "ExitThread" APIs to kill a thread ?
Thanks.
I have created a new thread to process a lenghthy loop so VB doesn't hang and I can still get on with my other work during the loop.
I used the CreateThread API and it works as expected . What I CAN'T do is end the thread prematurely so I can exit the loop if it is far too long.
I have tried the 2 APIs above to kill the thread but it crashes the whole app ! (:
I have have already seen the example given on AllAPI.net Site and searched the net for other more info but I can't find anything that works to end a thread !!!
Does anybody have a working example with "TerminateThread" or "ExitThread" APIs to kill a thread ?
Thanks.
ASKER
Thanks egl,
I am actually using VBA for this not VB so no real compiler.
I had already seen those links but I am afraid, they are not very clear\helpful...and as usual no real working code examples are given to illustrate the concepts (:
Any one has a concrete simple code that works ?
Regards.
I am actually using VBA for this not VB so no real compiler.
I had already seen those links but I am afraid, they are not very clear\helpful...and as usual no real working code examples are given to illustrate the concepts (:
Any one has a concrete simple code that works ?
Regards.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
OK, let me be a bit more specific.
Let's say that from within an Excel\VBA code, I want to seach the C drive for all files whose names include the letters "ab" and display the found files on a MsgBox.
Obviously this is a good scenario for wanting to run the loop in a seperate thread so the use can still work with XL while the lenghthy code is searching the C drive and all its Folders.
Using the CreateTread API all works fine for creating the new thread as well as running the lopp in its callback procedure. However if I wanted to abort the search , then I believe I would need to kill the Thread ...Right ?
To do so I am trying the TerminateThread as shown in the code below .Unfortuantely, I can't it to work. Worse still, the App crashes !!!
Code :
'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 blnRepeatSearch As Boolean
Public Sub AsyncThread()
Dim strFiles As String
Dim i As Long
With Application.FileSearch
If blnRepeatSearch Then
.NewSearch
End If
.LookIn = "C:"
.SearchSubFolders = True
.Filename = "ab"
.MatchTextExactly = False
.FileType = msoFileTypeAllFiles
.Execute
For i = 1 To .FoundFiles.Count
strFiles = strFiles & vbCrLf & .FoundFiles(i)
Next i
End With
MsgBox strFiles
hThread = 0
End Sub
Sub StartThread()
If blnRepeatSearch = False Then
blnRepeatSearch = True
End If
hThread = CreateThread _
(ByVal 0&, ByVal 0&, AddressOf AsyncThread, ByVal 0&, ByVal 0&, hThreadID)
CloseHandle hThread
End Sub
Sub KillThread() ''''''''''''''''''NOT WORKING !!!!!
If hThread <> 0 Then TerminateThread hThread, 0
End Sub
Any one knows how to make the code above work ??? ( ie: Make the killTread procedure work )
Regards.
Let's say that from within an Excel\VBA code, I want to seach the C drive for all files whose names include the letters "ab" and display the found files on a MsgBox.
Obviously this is a good scenario for wanting to run the loop in a seperate thread so the use can still work with XL while the lenghthy code is searching the C drive and all its Folders.
Using the CreateTread API all works fine for creating the new thread as well as running the lopp in its callback procedure. However if I wanted to abort the search , then I believe I would need to kill the Thread ...Right ?
To do so I am trying the TerminateThread as shown in the code below .Unfortuantely, I can't it to work. Worse still, the App crashes !!!
Code :
'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 blnRepeatSearch As Boolean
Public Sub AsyncThread()
Dim strFiles As String
Dim i As Long
With Application.FileSearch
If blnRepeatSearch Then
.NewSearch
End If
.LookIn = "C:"
.SearchSubFolders = True
.Filename = "ab"
.MatchTextExactly = False
.FileType = msoFileTypeAllFiles
.Execute
For i = 1 To .FoundFiles.Count
strFiles = strFiles & vbCrLf & .FoundFiles(i)
Next i
End With
MsgBox strFiles
hThread = 0
End Sub
Sub StartThread()
If blnRepeatSearch = False Then
blnRepeatSearch = True
End If
hThread = CreateThread _
(ByVal 0&, ByVal 0&, AddressOf AsyncThread, ByVal 0&, ByVal 0&, hThreadID)
CloseHandle hThread
End Sub
Sub KillThread() ''''''''''''''''''NOT WORKING !!!!!
If hThread <> 0 Then TerminateThread hThread, 0
End Sub
Any one knows how to make the code above work ??? ( ie: Make the killTread procedure work )
Regards.
You would want to use exitthread inside the AsyncThread callback, you must also make sure to exit any type of active loops before calling exit thread
ASKER
egl,
Any Idea how to apply that to the code ?
Thanks .
Any Idea how to apply that to the code ?
Thanks .
You can always get information from the remarks section of microsoft
TerminateThread
http://msdn.microsoft.com/library/en-us/dllproc/base/terminatethread.asp
ExitThread
http://msdn.microsoft.com/library/en-us/dllproc/base/exitthread.asp