How to check whether Thread is running? (D7)

After
MyThread.Terminate;
MyThread.WaitFor;
MyThread.Free;

WaitForSingleObject(MyThread.Handle, 0)<>WAIT_OBJECT_0
and MyThread<>nil still return True.
Is there any other way to check whether Thread is running?
3axapAsked:
Who is Participating?
 
HypoConnect With a Mentor Commented:
Your example with WaitForSignleObject should actually work fine... but as I said, you can not use that function after you have called MyThread.Free, so you must place WaitForSingleObject before MyThread.Free.

i.e.

if WaitForSingleObject(MyThread.Handle, 0) <> WAIT_OBJECT_0 then ... // If True the thread is still active
...
MyHandle.Free;


Another thing you can try, is to use the function GetExitCodeThread.

i.e.
var aExitCode : Cardinal;
...
if not GetExitCodeThread(MyThread.Handle, aExitCode) then
  raiseLastOsError;  
if aExitCode = STILL_ACTIVE then ...; // If true, your thread is still active...

In either case, you can not do these tests after MyThread.Free has been called.

if you want to force/kill your thread there is a function called TerminateThread, that works in the same way as GetExitCodeThread... but you should be aware of that this methods stops the thread directly, and may lead to the thread not being able to free it's resources (e.g. high possibility of memory leakage) ...

/Hypo
0
 
HypoCommented:
Hi,
So, after you've called MyThread.Free, you must not use that object anymore, since free triggers the deallocation of the memory for that object, that memory area that used to belong to the object might now be used by other objects and functions...

When you execute WaitForSingleObject(MyThread.Handle, 0); after you've freed the MyThread object, that handle you pass to the function is no longer valid, since the thread has been released... so that might be the reason why you get the unexpected result...

If you want to check that the thread has actually finished using WaitForSingleObject, you shall do that check before you free the MyThread object... also when you free the object I suggest you start using the function FreeAndNil, so that you don't keep pointers to objects that have been deallocated.

MyThread.Terminate;
MyThread.WaitFor;
// Check if the thread has actually terminated...
If WaitForSingleObject(MyThread.Handle, 0)<>WAIT_OBJECT_0 then ... ;
// Free the thread and set it's pointer to nil...
FreeAndNil(MyThread);


/Hypo
0
 
3axapAuthor Commented:
Actually, I'd like to be sure that thread is terminated.
When thread is running WaitForSingleObject(MyThread.Handle, 0)<>WAIT_OBJECT_0 returns True and after I terminated and freed it WaitForSingleObject(MyThread.Handle, 0)<>WAIT_OBJECT_0 still returns True.
That is where I'm lost. I thought it should return False.

MyThread.Terminate; sets a flag for the thread to finish its routine and stop. But sometimes MyThread is hanging up and I want to set a timer to let it finish and if not use TerminateThread(MyThread.ThreadID, 0);
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
HypoCommented:
The thing is that if you free the thread, then you cant check if it's still running, because once you call MyThread.Free, the handle to the thread becomes invalid... and you need a valid handle for the WaitForSingleObject call...

That is what I tried to explain, If you need to check if the thread is still running, you need to do that before you call MyThread.free...

/Hypo
0
 
3axapAuthor Commented:
I can rephrase my question if I may.
How to get confirmation that thread I want to terminate is actually terminated?
0
 
HypoCommented:
sorry, typo in my last post...

MyHandle.Free should actually be MyThread.Free...

/Hypo
0
 
developmentguruPresidentCommented:
You can also assign your thread on OnTerminate event.  This will call your code when the thread is terminated.  This previously answered question demonstrates using OnTerminate with multiple threads.

http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_27317530.html
0
 
3axapAuthor Commented:
I checked GetExitCodeThread.
Could you please revise my code below.
Thanks.

procedure mainForm.ThrdTerminate;
var
  aExitCode: Cardinal;
begin
  MyThread.Terminate;
  WaitForSingleObject(MyThread.Handle, 10000);
  if not GetExitCodeThread(MyThread.Handle, aExitCode) then raiseLastOsError;
  if (aExitCode=STILL_ACTIVE) then TerminateThread(MyThread.ThreadID, 0) 
  	else MyThread.Free;
end;

Open in new window

0
 
HypoCommented:
I would add one change to your code, and that is to always free at the end... but in large it looks ok...

However, If you are having problems with your thread not being terminated when you request it with MyThread.Terminate, that indicates that you have something wrong in your thread that you ought to correct... because as I said in an earlier post, when you force a thread to terminate using TerminateThread, there is a high risk of memory leakage, and you should avoid using it if possible...

Are you perhaps doing any Synchronize calls in your thread, or any other calls to WaitForSingleObject where you wait for Events, Mutexes or Semaphores to be signaled? because that might be the reason why your thread isn't always terminating properly.


procedure mainForm.ThrdTerminate;
var
  aExitCode: Cardinal;
begin
  MyThread.Terminate;
  WaitForSingleObject(MyThread.Handle, 10000);
  if not GetExitCodeThread(MyThread.Handle, aExitCode) then raiseLastOsError;
  if (aExitCode=STILL_ACTIVE) then TerminateThread(MyThread.ThreadID, 0);
  FreeAndNil(MyThread);
end;

Open in new window


/Hypo
0
 
3axapAuthor Commented:
I'm trying to find solution for thread also. It just that InternetOpenUrl in thread is hanging if server on the other side is down. I'll use TerminateThread only when terminate application.
0
 
HypoCommented:
Ok, so there is no timeout that can be set for that function?

Perhaps you already know this, but there is one function you can use to test if you can connect to a server called InternetCheckConnection... have you tried calling that one before you call InternetOpenUrl?

/Hypo
0
 
3axapAuthor Commented:
I thought InternetCheckConnection is telling whether I'm connected to the Internet.
I'll look at it. Thanks.
0
 
3axapAuthor Commented:
Thanks a lot.
0
All Courses

From novice to tech pro — start learning today.