1

How can I kill task on Demand? When i do this:

CreateTask(TWorker.Create())
                    .Invoke(@TWorker.Execute)
                    .MonitorWith(FEventMonitor)
                    .SetParameter('ThreadID',i)
                    .Join(FWorkers)
                    .WithLock(FLockToken).Schedule(GlobalOmniThreadPool).WaitFor(0);



if Assigned(FWorkers) then begin
  // FWorkers.TerminateAll; freeze 
     FWorkers.TerminateAll(0); // freeze
     FWorkers:=nil;
     GlobalOmniThreadPool.CancelAll;
     Log('All tasks stopped',Form1.Memo1);
  end
   else begin
   Log('Nothing to stop',Form1.Memo1);
  end;

The main application freezes. It doesn't really matter whether, do I run it outside or inside the debugger even if there is only 1 task running.

Because I don't want to put this code 30 times in the task. Frustrating for the users and programers.

If GlobalCancel=true then
Task.Terminate;

There should be a way to do this globally without this.

  • You might not want to do it, but that's how you cancel tasks. – David Heffernan Dec 08 '12 at 21:41
  • @DavidHeffernan Its not the way it should be done. I have a task that has over 1000 lines of code. Who is going to check if the task is terminated on every line.. –  Dec 08 '12 at 21:44
  • 1
    No really, that's how it's meant to be done. Not every line. But often enough. Pre-emptively killing tasks or threads leads to shared objects being in ill-defined state. You leak memory. You leave locks in an inconsistent state. You'll have deadlocks and so on. – David Heffernan Dec 08 '12 at 21:51
  • @DavidHeffernan Acording to gabr's forum this feature wasn't even implemented yet and the library is at 3.00 version.. "Currently, OTL infrastructure doesn't support killing threads on demand." –  Dec 08 '12 at 22:18
  • 3
    Yes, I think Primoz understands very well why it's not sensible to pre-emptively kill tasks/threads. – David Heffernan Dec 08 '12 at 22:19
  • @DavidHeffernan Well what if the task doesn't respond to the cancel token? I don't create anything within the task and free everything when it is terminated Create()/Destroy(). So it can be perfectly fine to terminated it on demand. Leting users wait 10 minutes for 1 task to finish is really bad. Users want the option I guess they won't get it for a while along the 60 MAX limit. –  Dec 08 '12 at 22:21
  • Don't shoot the messenger. I'm just telling you how it is. Check your cancellation token regularly. – David Heffernan Dec 08 '12 at 22:26
  • @DavidHeffernan Yes solution was your proposal. But at the cost of more than 500 extra lines of code.. –  Dec 08 '12 at 23:33
  • Break your 1000 lines into logical parts, name them tasks and run this tasks within a loop. Before every task you can check the cancel flag. – Sir Rufo Dec 08 '12 at 23:43
  • @SirRufo Yes I have this done already but I mean this will be very hard to maintain. Especially if the project grows.. –  Dec 08 '12 at 23:46
  • if you have done, then you won't have 500 extra lines of code. Why hard to maintain? You will only have small pieces of code. – Sir Rufo Dec 08 '12 at 23:53
  • I think that's right. With some good structure, you'll never need that many cancellation token checks. I do agree though that checking cancellation tokens is annoying. – David Heffernan Dec 09 '12 at 00:03
  • And why complaining about the ThreadPoolSize 60? Your thread (long running) was not in mind to run with ThreadPool. Check this one http://stackoverflow.com/questions/13458029/why-is-omnithreadlibrary-limited-to-60-threads-when-nets-limit-is-32768 – Sir Rufo Dec 09 '12 at 00:09
  • @SirRufo However you want to sensionalize it we need this feature. Its up to the programmer how he decides to use it. He may use it bad or good. But it should be his choice. –  Dec 09 '12 at 01:16
  • You should ask your question in OTL Forum – Sir Rufo Dec 09 '12 at 01:42

0 Answers0