c# - Cancellable threads queue -


in our geometry edition software, when user changes parameter(s) of geometry (for example size or position of geometric figure) , list of "interesting" main points geometry should obtained , shown in table. because obtaining these points can last seconds sometimes, decided use backgroundworkers.

the first problem found had control when these points asked calculated because wanted show ones belonging last change in geometry , not last ones calculated in backgroundworkers. solved issue adding 2 datetime variables: 1 saves time when actual shown points queued calculated , other controls when actual calculation in actual thread sent. when calculation done compare 2 dates , overwrite actual list of points if newer. following code (in user control datagridview):

    /// <summary>     ///     updates main points.     /// </summary>     /// <param name="track">track.</param>     public void updatemainpoints(track track)     {         track = track;         if (backgroundworker.isbusy)         {             backgroundworker = new backgroundworker();             backgroundworker.dowork += backgroundworker_dowork;         }         backgroundworker.runworkerasync();     }      private void backgroundworker_dowork(object sender, doworkeventargs e)     {         try         {             invoke((methodinvoker) showmessage);         }         catch         {             // ignored because can called when window closed , can not controlled otherwise         }         datetime jobqueuetime = datetime.now;         list<mainpoint> mainpoints = track.getmainpoints(_currentunit);         if (datetime.compare(jobqueuetime, _shownpointsqueuetime) > 0)         {             //it updates actual main point if operation last 1 calculated             _mainpoints = new list<mainpoint>(mainpoints);             _shownpointsqueuetime = jobqueuetime;         }         //updates time in shown points queued calculated         try         {             invoke((methodinvoker)hidemessageandshowmainpoints);         }         catch         {             // ignored because can called when window closed , can not controlled otherwise         }     } 

the problem find there can lot of threads running in background although results not shown. have searched , not clear if , how can "kill" threads. maybe can control somehow if these. suggestion?

taffer has advice there, since you've got many background worker threads it's gotten more complicated.

if want stop of them @ once, pass cancellationtoken thread through doworkeventargs.argument, can check if token cancelled in backgroundworker_dowork , outside backgroundworker_dowork you'd cancel 1 cancellationtokensource.

if need kill them on individual basis, you'll need keep track of them. put them in kind of collection. can still call cancelasync(), inside of backgroundworker_dowork it's hard know thread yours. suppose pass reference thread through doworkeventargs.argument , check cancellationpending on that. i've not tried seems work.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -