.net - Notify multiple background workers with a property change C# -
i have global variable called: string tweet;
i run several background workers, nothing wait on value change of tweet variable. run function called: processtweet( object sender, mycustomeventargs args )
my question best way handle property changed event background workers, , later process results based on tweet value , argument passed processtweet function.
i tried take @ inotifypropertychanged not sure how handle onvaluechange event each background worker. run same processtweet function once or each background worker run instance of function?
edit:
private itweet _lasttweet; public itweet lasttweet {    { return this._lasttweet; }    set    {       this._lasttweet = value;          } } still not sure how handle property change event best way ^
and below rest of code
    private void bgworker_dowork(object sender, doworkeventargs e)     {         mycustomclass mycustomclass = e.argument mycustomclass;         //here want listen on lasttweet value change event , handle     }      list<backgroundworker> listofbgworkers = new list<backgroundworker>();     private backgroundworker createbackgroundworker()     {         backgroundworker bgworker = new backgroundworker();         //add dowork etc..         bgworker.dowork += new system.componentmodel.doworkeventhandler(bgworker_dowork);          return bgworker;     }      private void buttonstart_click(object sender, eventargs e)     {         (int = 0; < 10; i++)         {             //create background workers             var bgworker = createbackgroundworker();             listofbgworkers.add(bgworker);             //get mycustomclass value;             var mycustomclass = somefunction();             bgworker.runworkerasync(mycustomclass);          }       } 
ok - here's small console app demonstrates think you're trying do.
- it creates 'source of tweets' in thread.
- you can subscribe 'source' , notified when new tweet 'arrives'.
- you create tweethandlers have internal queues of tweets process
- you subscribe these tweethandlers source
- when new tweet arrives, added queues of all tweethandlers event subscription
- the tweethandlers set run in own tasks. each tweethandler has own delegate performing customizable action on tweet.
the code follows:
interface itweet {     object somedata { get; } }  class tweet : itweet {     public object somedata { get; set; } }  class tweetsource {     public event action<itweet> newtweetevent = delegate { };      private task tweetsourcetask;      public void start()     {         tweetsourcetask = new taskfactory().startnew(createtweetsforever);     }      private void createtweetsforever()     {         while (true)         {             thread.sleep(1000);             var tweet = new tweet{ somedata = guid.newguid().tostring() };             newtweetevent(tweet);         }     } }  class tweethandler {     public tweethandler(action<itweet> handletweet)     {         handletweet = handletweet;     }      public void addtweettoqueue(itweet tweet)     {         queueoftweets.add(tweet);     }      public void handletweets(cancellationtoken token)     {         itweet item;         while (queueoftweets.trytake(out item, -1, token))         {             handletweet(item);         }     }      private blockingcollection<itweet> queueoftweets = new blockingcollection<itweet>();     private action<itweet> handletweet; }  class program {     static void main(string[] args)     {         var handler1 = new tweethandler(tweethandlemethod1);         var handler2 = new tweethandler(tweethandlemethod2);          var source = new tweetsource();         source.newtweetevent += handler1.addtweettoqueue;         source.newtweetevent += handler2.addtweettoqueue;          // start task threads (2 of them)!         var tokensource = new cancellationtokensource();         var token = tokensource.token;         var taskfactory = new taskfactory(token);         var task1 = taskfactory.startnew(() => handler1.handletweets(token));         var task2 = taskfactory.startnew(() => handler2.handletweets(token));          // fire source         source.start();          thread.sleep(10000);         tokensource.cancel();     }      static void tweethandlemethod1(itweet tweet)     {         console.writeline("did action 1 on tweet {0}", tweet.somedata);     }      static void tweethandlemethod2(itweet tweet)     {         console.writeline("did action 2 on tweet {0}", tweet.somedata);     } } the output looks this:
did action 2 on tweet 892dd6c1-392c-4dad-8708-ca8c6e180907 did action 1 on tweet 892dd6c1-392c-4dad-8708-ca8c6e180907 did action 2 on tweet 8bf97417-5511-4301-86db-3ff561d53f49 did action 1 on tweet 8bf97417-5511-4301-86db-3ff561d53f49 did action 2 on tweet 9c902b1f-cfab-4839-8bb0-cc21dfa301d5 
Comments
Post a Comment