OmniThreadLibrary forum
News: SMF - Just Installed!
 
*
Welcome, Guest. Please login or register. May 17, 2012, 06:19:56 PM


Login with username, password and session length


Pages: [1]   Go Down

Author Topic: Correct way to terminate tasks?  (Read 182 times)

Qmodem

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Correct way to terminate tasks?
« on: February 06, 2012, 10:14:48 AM »

What is the correct procedure to terminate tasks if you need to cancel what they are doing?

I have a procedure that starts two tasks that can take 20-30 seconds each as they each process a dataset and create an output file.  If I want to cancel the output and close the dialog I have coded this in my formclosequery:

Code: [Select]
procedure TCCC100.FormCloseQuery(
  Sender       : TObject;
  var CanClose : Boolean);
begin
  inherited;
  GlobalOmniThreadPool.CancelAll;

  if Assigned(InventoryTask) then
    InventoryTask.Terminate;

  if Assigned(PartTask) then
    PartTask.Terminate;

  if Assigned(CountTasks) then
    while CountTasks.Value > 0 do
      Application.ProcessMessages;

  PartTask      := nil;
  InventoryTask := nil;
  Terminated    := true;
end;

The dialog gets stuck in an endless loop waiting for CountTasks.Value to reach 0;

The task is setup as such:
Code: [Select]
  InventoryTask := CreateTask(LoadInventoryRecords, 'LoadInventoryRecords')
    .OnMessage(InventoryHandleTaskMessage)
    .OnTerminated(InventoryHandleTaskTerminated)
    .SetParameter('TaskNumber', 1)
    .Schedule(GlobalOmniThreadPool);

and the OnTerminate method is:

Code: [Select]
procedure TCCC100.InventoryHandleTaskTerminated(
  const task : IOmniTaskControl);
begin
  LogMessages(Format('[%d/%s] %d|%s', [task.UniqueID, task.Name, 1, 'Inventory finished.']));
  CountTasks.Decrement;
end;

When the FormCloseQuery calls InventoryTask.Terminate, does that not tell the task to run its Terminated method and then decrement CountTasks as shown above? From what I can tell, it skips the OnTerminate method so i get stuck in the loop.  If I comment out the loop waiting for CountTasks to reach zero, I get an exception when I assign nil to the two tasks created.

Guess I need a lesson in cleaning up tasks!

Thanks in advance.

John
Logged

Primoz Gabrijelcic

  • Administrator
  • Hero Member
  • *****
  • Posts: 569
    • View Profile
    • Email
Re: Correct way to terminate tasks?
« Reply #1 on: February 06, 2012, 02:09:31 PM »

You are doing everything by the book but you have stumbled upon a bug. I'll fix it as soon as possible - but first I have to understand what the real cause of the problem is.

For the time being, a workaround would be to call CountTasks.Decrement as a last thing in LoadInventoryRecords method (and other task methods).
Logged

Primoz Gabrijelcic

  • Administrator
  • Hero Member
  • *****
  • Posts: 569
    • View Profile
    • Email
Re: Correct way to terminate tasks?
« Reply #2 on: February 07, 2012, 04:37:47 AM »

Fixed in the SVN. Thanks for finding this problem, it was a nasty one.
Logged

Qmodem

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: Correct way to terminate tasks?
« Reply #3 on: February 07, 2012, 06:36:18 AM »

Thank you.  I will update from SVN and test today.

Cheers!

John
Logged

Primoz Gabrijelcic

  • Administrator
  • Hero Member
  • *****
  • Posts: 569
    • View Profile
    • Email
Re: Correct way to terminate tasks?
« Reply #4 on: February 07, 2012, 06:41:05 AM »

Please report back with results.
Logged

Qmodem

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: Correct way to terminate tasks?
« Reply #5 on: February 07, 2012, 07:05:52 AM »

I just tested the fix and it works perfectly.

Thanks again.

John
Logged

Primoz Gabrijelcic

  • Administrator
  • Hero Member
  • *****
  • Posts: 569
    • View Profile
    • Email
Re: Correct way to terminate tasks?
« Reply #6 on: February 07, 2012, 07:18:21 AM »

Great! Again, thanks for the report - this bug would have bitten me sooner or later.
Logged
Pages: [1]   Go Up
 
 

Powered by MySQL Powered by PHP Powered by SMF 2.0.2 | SMF © 2006-2009, Simple Machines LLC

Valid XHTML 1.0! Valid CSS! Dilber MC Theme by HarzeM