c# - how to propagate some data to main process from TPL tasks while tasks are running -


i have situation create list of long running tasks monitors system/network resources , sends email, logs txt file, , calls web service when conditions met. begins monitoring again. these tasks created in windows service , hence running time.

i want them raise events or notify parent class (which created them) , performs 3 operations mentioned above instead of each object in tasks doing itself.

and how can controlled single task uses parent class's method @ single time. email , web service call involved, 2 concurrent requests may beak code.

update

these watchers of 3 types, each implements following interface.

public interface iwatcher {     void beginwatch();         } 

classes implement

//this watcher responsible watching on sql query result public class dbwatcher : iwatcher {     ....     void beginwatch()     {         //here timer created contiously checks sql query result.         //and call service, send email , log file          timer watchiterator = new timer(this._intervalminutes * 60000);         watchiterator.elapsed += new elapsedeventhandler(_watchiterator_elapsed);         watchiterator.start();     }      void _watchiterator_elapsed(object sender, elapsedeventargs e)     {         //1. check query result         //3. call service, send email , log file if result not expected         //i have done work part!          //and can functions follows .. should simple.         //*********************         //sendemail();         //logintofile();         //callservice();          //but want above 3 methods present in 1 place dont have replicate same functionality in different watcher.         //one approach create seperate class , wrape above mentioned functions in it, create instance of class here , call them.         //second option, interested in dont know how do, have functionality in parent class creates tasks , have each watcher use here ...     }     .... }   //this watcher responsible watching on folder public class folderwatcher : iwatcher {     ....     void beginwatch()     {          ///same above     }     .... } 

first create list xml file. can contain multiple instances of dbwatcher continously watch different query result , folderwatcher continously watch different folders continously.

after list created, call following function call create separate task. call function many times create different set of watchers.

    private void _createwatcherthread(iwatcher wat, cancellationtokensource cancellationtoken)     {         //this represents watcher watch specific area activities         iwatcher watcher = wat.copy();         bool haswatchbegin = false;         try         {             //run forever             (;;)             {                 //dispose watcher , stop thread if cancel token has been issued                 if (cancellationtoken.iscancellationrequested)                 {                     ((idisposable)watcher).dispose();                     break;                 }                 else if (!haswatchbegin)                 {                     //this method of watcher class creates timer.                     //continously check status after few minutes...                      //timer's elapsed method in watcher object send mail                      //& call service etc update admin of current status of watcher.                     //this called once in watcher!                     watcher.beginwatch();                     haswatchbegin = true;                 }             }         }         catch (exception ex)         {             //watcher has thrown exception.              //again, following operations             //*********************             //sendemail();             //logintofile();             //callservice();         }     } 

provided make email, logging & webservice calls threadsafe can pass references code sends each of these sinks closure (here's jon skeet's excellent explanation of c# closures) monitoring tasks. here's example need launch multiple tasks:

... void email(string message){}  void log(string message){}  void callwebservice(string message){}   void runmonitoringtask() {     var task = task.taskfactory.startnew(() =>     {         string message = monitor();         if( shouldnotify(message))            {               email(mesasge);               log(message);               callwebservice(message);            }     } ) } ... 

edit

vs. infinite monitor loop triggering tasks when necessary:

... void email(string message){}  void log(string message){}  void callwebservice(string message){}   void monitor() {     while(true)     {           string message = monitor();           if(shouldnotify(message))           {                var task = task.taskfactory.startnew(() =>               {                   email(mesasge);                   log(message);                   callwebservice(message);               }          }     } ) } ... 

as far how implement these classes, i'd recomend approach each of these sinks accepts message & offloads it's own processing thread/task avoid blocking monitoring tasks & holding other notifications.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -