Board index » delphi » Thread will run on NT but hangs on W98

Thread will run on NT but hangs on W98

I have asked a lot of experienced Delphi programmers about this question and
no one seems to be able to help.  Maybe one of you can help me out:)

procedure TForm1.Thread1Terminate;
begin
  Thread1Done:=True;
end;

for i:=0 to count-1 do
begin
  Thread1Done:=False;

  with MyThread.Create(ParamsgoHere) do OnTerminate:=Thread1Terminate;

  while not Thread1Done do Application.ProcessMessages;
end;

Above is my general code structure...  Basically I iterate through a
TLayoutListView and use the thread to process items contained therein.  This
works perfectly on Win NT.

When I run the EXE on Win 98, the first iteration runs flawlessly but the
second iteration ends in disaster.  The second time through, the thread
appears to be created but never executes.  The system just hangs on the while
not loop waiting for a hung thread to terminate.  The problem only occurs in
Win 98.

On the advice of some fellow programmers I commented out everything in the
execute method of the thread to see if something in the thread was causing the
problem.  An "empty" execute method still results in a hung thread in W98.

Here is the general code for my Thread Constructor:

Constructor MyThread.Create(ParamsHere);
begin
  inherited Create(False);
  <Params Here>
  FreeonTerminate:=True;
end;

I did find a sort of workaround for the problem if I add a showmessage to the
end of the loop like this:

for i:=0 to count-1 do
begin
  Thread1Done:=False;

  with MyThread.Create(ParamsgoHere) do OnTerminate:=Thread1Terminate;

  while not Thread1Done do Application.ProcessMessages;

  ShowMessage('Done Waiting');
end;

This will run on Win 98!  The problem is I have to click OK after each
iteration.

Do you have any ideas?  Again the code works flawlessly on NT but on Win 98 it
just hangs on the second iteration waiting for the hung thread to terminate.

Please e-mail me at marie...@slu.edu with your reply if you don't mind:)

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    

 

Re:Thread will run on NT but hangs on W98


Quote
marie...@slu.edu wrote in message <7e9p7i$61...@nnrp1.dejanews.com>...
>I have asked a lot of experienced Delphi programmers about this question
and
>no one seems to be able to help.

You may have several problems here.

(1)  It's not clear to me why you're using a thread.  The main loop waits
for the one thread to finish, so you're not getting much
multi-processing.  In fact your while loop wastes quite a bit of time
polling.    If you're doing things this way to keep the program
responsive to user input, there are better ways to do this.

You could put a few calls to "Application.Processmessages" in the called
procedure (not too few, not tio many..) in order to keep the GUI feeling
alive.

(2) Putting the "create thread" call in the "with" may be a bit
dangerous, as depending on optimization, it might get executed zero, one,
or more times.

(3)  You're depending on the "OnTerminate" handler getting called by the
thread.   If the thread get an exception, this may not happen.

(4)  You're depending on the Thread.Create call to NOT immdiately run the
thread, but have the main loop continue so it can set the OnTerminate :=
xxx.
If the thread runs immediately, the onterminate hander won't be set and
if the thread runs and terminates in one time-slice the thread will
finish before your main loop ever gets a chance to set the onterminate
handler.
Win32 API doesnt gurantee any particular order of execution, so it's
quite likely NT and Win-95 are going to schedule threads differently,
leading to the symptoms you see.

You need to either create the thread in "suspended" state, so you have a
good chance to set the onterminate handler, or set OnTerminate FIRST
THING in the thread itself.

 Maybe one of you can help me out:)

Quote

>procedure TForm1.Thread1Terminate;
>begin
>  Thread1Done:=True;
>end;

>for i:=0 to count-1 do
>begin
>  Thread1Done:=False;

>  with MyThread.Create(ParamsgoHere) do OnTerminate:=Thread1Terminate;

>  while not Thread1Done do Application.ProcessMessages;
>end;

>Above is my general code structure...  Basically I iterate through a
>TLayoutListView and use the thread to process items contained therein.
This
>works perfectly on Win NT.

>When I run the EXE on Win 98, the first iteration runs flawlessly but
the
>second iteration ends in disaster.  The second time through, the thread
>appears to be created but never executes.  The system just hangs on the
while
>not loop waiting for a hung thread to terminate.  The problem only
occurs in
>Win 98.

>On the advice of some fellow programmers I commented out everything in
the
>execute method of the thread to see if something in the thread was
causing the
>problem.  An "empty" execute method still results in a hung thread in
W98.

>Here is the general code for my Thread Constructor:

>Constructor MyThread.Create(ParamsHere);
>begin
>  inherited Create(False);
>  <Params Here>
>  FreeonTerminate:=True;
>end;

>I did find a sort of workaround for the problem if I add a showmessage
to the
>end of the loop like this:

>for i:=0 to count-1 do
>begin
>  Thread1Done:=False;

>  with MyThread.Create(ParamsgoHere) do OnTerminate:=Thread1Terminate;

>  while not Thread1Done do Application.ProcessMessages;

>  ShowMessage('Done Waiting');
>end;

>This will run on Win 98!  The problem is I have to click OK after each
>iteration.

>Do you have any ideas?  Again the code works flawlessly on NT but on Win
98 it
>just hangs on the second iteration waiting for the hung thread to
terminate.

>Please e-mail me at marie...@slu.edu with your reply if you don't mind:)

>-----------== Posted via Deja News, The Discussion Network ==----------
>http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own

Re:Thread will run on NT but hangs on W98


Quote
marie...@slu.edu wrote:
> I have asked a lot of experienced Delphi programmers about this question and
> no one seems to be able to help.  Maybe one of you can help me out:)

Like George said, this is really not the way to use threading.  You have main
thread effectively waiting for the child thread to finish.  Not a lot of point in
that.  If you really want to use a thread, then

1) create the thread in suspended mode
2) set the Onterminate method
3) start the thread

Rather than having a loop to start the threads in sequence, use the On terminate
to start the next thread

procedure StartThread;
begin
    with MyThread.Create(ParamsgoHere) do
    begin
        OnTerminate:=Thread1Terminate;
        Resume;
    end;
end;

procedure Thread1Terminate(Sender:TObject);
begin
    if Iteration<count then
    begin
        inc(iteration);
        StartThread;
    end;
end;

{main application}
    iteration:=0;
    StartThread;
{do not call processmessages}

--
Bob Lee
High Performace Delphi - http://www.econos.com/optimize/

Re:Thread will run on NT but hangs on W98


Oh dear.

You really shouldn't wait for threads in this manner. Just set the
thread going, and get it to notify you by posting a message when it's
done, or some such equivalent mechanism. Remember not to fiddle with
common state in the thread and the main VCL thread.... if you do, then
please use mutexes or semaphores appropriately.

I suspect you don't really understand how threads work, given this
example, and you may wish to rethink what you're trying to achieve.

MH.

Quote
marie...@slu.edu wrote:

> I have asked a lot of experienced Delphi programmers about this question and
> no one seems to be able to help.  Maybe one of you can help me out:)

> procedure TForm1.Thread1Terminate;
> begin
>   Thread1Done:=True;
> end;

> for i:=0 to count-1 do
> begin
>   Thread1Done:=False;

>   with MyThread.Create(ParamsgoHere) do OnTerminate:=Thread1Terminate;

>   while not Thread1Done do Application.ProcessMessages;
> end;

--
Martin Harvey.
http://www.harvey27.demon.co.uk/mch24/
Takeoff is optional.
Landing (sooner or later) is mandatory.

Other Threads