c# - How to abort a list of tasks from the outside -


i still new threading model in .net 4.5 (or threading in general matter). trying build set of abstract classes in order implement framework multithreading series similar tasks. rules first line of input tells how many problems in set, , following lines contain input each problem. how many lines of input make set can vary 1 task next, consistent given type of problem. each problem have single line of text output.

the goal able inherit these classes , have worry implementing createworker , dowork methods. ui call processinput things started, , writeoutput list off results.

the basic template have far is:

public abstract class wrapper {     protected int n;     protected workerbase[] problemset;     protected list<task> tasks;      public virtual void processinput(textreader input)     {         string line = input.readline();         n = int.parse(line);         problemset = new workerbase[n];         tasks= new list<task>(n);         (int index = 0; index < n; index++)         {             workerbase t = createworker(input);             tasks.add(t.doworkasync());             problemset[index] = t;         }     }      public virtual bool writeoutput(textwriter outputstream, int timeout)     {         bool complete = task.waitall(tasks.toarray(), timeout);          (int index = 0; index < n; index++)         {             outputstream.writeline(problemset[index].result);         }          return complete;     }      protected abstract workerbase createworker(textreader inputstream);     protected abstract class workerbase     {         public string result         {             get;             protected set;         }         protected abstract void dowork();         public task doworkasync()         {             return task.run(() => dowork());         }     } }  

as is, meeting requirements. add abort functionality wrapper class capable of halting of threads. question is, how work abort functionality structure in such way ui can abort process if cancel button clicked? preferably without having write code in dowork implementation.

i have found patterns pass in cancelation token in order abort task, every version have found require dowork check state of token , abort itself. want way abort dowork outside method.

aborting running code can done in 2 ways (well, kinda 3 if include "it never gets started queue of things do", suspect happens cancelled tasks), 1 of sane:

  • the work periodically checks "abort" flag , terminates cleanly
  • you abort entire thread

the first should doing; in cases works fine. second virtually never idea. time should consider when entire process terminally ill want put down (and out of misery) fast possible. after aborting threads, there's fair chance you've left system in irrecoverable position, should never used sane operations. also: you'd need have reference each thread involved, don't have here.

so: leaves first case. frankly, think should , check flags here. however, approach can work if worker needs periodically values somewhere, have input properties / methods / etc check there, , raise well-known exception, i.e. worker might exception when accessing foo.count (or similar).

it however, preferable formally check cancellation and report cancelled, status of task can maintained. guess catch block, though.


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -