Link to home
Start Free TrialLog in
Avatar of 3axap
3axap

asked on

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?
Avatar of Hypo
Hypo
Flag of Sweden image

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
Avatar of 3axap
3axap

ASKER

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);
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
Avatar of 3axap

ASKER

I can rephrase my question if I may.
How to get confirmation that thread I want to terminate is actually terminated?
ASKER CERTIFIED SOLUTION
Avatar of Hypo
Hypo
Flag of Sweden image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
sorry, typo in my last post...

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

/Hypo
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.

https://www.experts-exchange.com/questions/27317530/Showing-progress-from-multiply-threads.html
Avatar of 3axap

ASKER

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

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
Avatar of 3axap

ASKER

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.
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
Avatar of 3axap

ASKER

I thought InternetCheckConnection is telling whether I'm connected to the Internet.
I'll look at it. Thanks.
Avatar of 3axap

ASKER

Thanks a lot.