Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Terminating a second thread in a service app

Status
Not open for further replies.

Griffyn

Programmer
Jul 11, 2002
1,077
AU
Hi all,

I've worked with threads a lot, but I'm stumped by how to do what seems to be a normal setup.

I have a service application that launches a new thread (TCopyThread) that copies a file using TFileStream objects. The main service thread only calls a TTimer object every so often to create another TCopyThread when the last one has finished. (Down the track I'll work on allowing more than one TCopyThread run, plus there could be more different types of threads.) The thread can be terminated in response to
[ul]
[li]the service being stopped[/li]
[li]the service thread deciding using it's own logic that the thread should be terminated[/li]
[li]the TCopyThread finishing it's copy process[/li]
[/ul]

At the moment I have an event attached to TCopyThread.OnTerminate that runs cleanup code and provides feedback on how far the thread went with the copy operation.

That seems to work for everything except when the service is stopped. I have code similar to
Code:
[b]procedure[/b] TCopyBak2.ServiceStop(Sender: TService; [b]var[/b] Stopped: Boolean);
[b]begin[/b]
  [b]if[/b] Assigned(FCopyThread) [b]then[/b]
  [b]begin[/b]
    FCopyThread.Terminate;
    FCopyThread.WaitFor;  [navy][i]// this hangs the service
[/i][/navy]  [b]end[/b];
  [navy][i]// snip
[/i][/navy]  Stopped := True;
[b]end[/b];

Except it hangs where indicated. I'm pretty sure this is because the event I've attached to TCopyThread.OnTerminate is called using Synchronize internally by the TThread object, meaning that it doesn't get called because the main service thread is stuck waiting for the thread to finish. That doesn't make much sense to me, so perhaps I'm misunderstanding why it's not working.

I've tried using messages and having the thread post a message to the service thread handle (ServiceThread.Handle) when it's finished and without attaching anything to the OnTerminate event. The message method in the service thread then does the cleanup. In the ServiceStop method I used [tt]WaitMessage[/tt] instead of [tt]WaitFor[/tt] thinking it would wait until the message was received from TCopyThread, but it never processed it.

What's the go on getting this to work?
 
Well, I got it working, although a bit messily.
Code:
[b]procedure[/b] TCopyBak2.ServiceStop(Sender: TService; [b]var[/b] Stopped: Boolean);
[b]var[/b]
  t : TThread;
[b]begin[/b]
  [b]if[/b] Assigned(FCopyThread) [b]then[/b]
  [b]begin[/b]
    FCopyThread.Terminate;  [navy][i]// this interrupts the thread
[/i][/navy]    FCopyThread.WaitFor;
    t := FCopyThread;
    CleanupThread(FCopyThread);
    t.Free;
  [b]end[/b];
  [navy][i]// snip[/i][/navy]
  Stopped := True;
[b]end[/b];
Code:
[b]procedure[/b] TCopyBak2.CleanupThread(Sender: TObject);
[b]begin[/b]
  [b]try[/b]
    [navy][i]// do cleanup/finalization stuff
[/i][/navy]  [b]finally[/b]
    FCopyThread := [b]nil[/b];
  [b]end[/b];
[b]end[/b];

And for my TCopyThread object
Code:
[b]procedure[/b] TCopyThread.Execute;
[b]begin[/b]
  [b]try[/b]
    [navy][i]// snip
[/i][/navy]  [b]finally[/b]
    [b]if[/b] FService.Status <> csStopPending [b]then[/b]
    [b]begin[/b]  [navy][i]// FService = main service thread[/i][/navy]
      OnTerminate := FOnFinish; [navy][i]// FOnFinish = CleanupThread method[/i][/navy]
      FreeOnTerminate := True;
    [b]end[/b];
  [b]end[/b];
[b]end[/b];

That seems to work, everything gets called and nothing hangs. Messy though. I'd still appreciate it if anyone could help clean it up for me!
 
never use Waitfor in Delphi, it is bugged and prone to deadlocks. use semaphores instead.

small example:


-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top