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 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 ?


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Curious to know if you tested when your application was compiled and not from the IDE. Your application should crash if you compiled in native code. If you compile in p-code you have a better chance that createthread would work.

You can always get information from the remarks section of microsoft


RAFAAJAuthor Commented:
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 ?

One terminates the thread and the otherone will exit the thread, if your familiar with TerminateProcess and WM_CLOSE then you can think of it like that you never want to Terminate a process instead you want to close the process nicely by sending WM_CLOSE message. Any active threads that you want to exit for cleanup use exitthread if the thread doesn't close than you can call terminate thread but remember as microsoft says this is dangerouse depending on what operations are being performed in the thread which could lead to corruption. For example if you open the thread and withing the thread you don't want to proceed because something didn't meet a specific criteria then call exitthread if its a major operation exitthread might not work and then you will be have to force the termination of the thread which is where TerminateThread comes into play.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

RAFAAJAuthor Commented:
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
        End If
        .LookIn = "C:"
        .SearchSubFolders = True
        .Filename = "ab"
        .MatchTextExactly = False
        .FileType = msoFileTypeAllFiles
        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 )


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
RAFAAJAuthor Commented:

Any Idea how to apply that to the code ?

Thanks .
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.