c# - pausing and stopping threads -


i have following code working dirty. code fine except part added: pause , stop button. i'm new c# apreciated.

    private void pause_button_click(object sender, eventargs e)     {         start = false; pause = true; stop = false;         guiupdate();         pauseevent.reset();     }      private void stop_button_click(object sender, eventargs e)     {         if (pause == true)         {             pauseevent.set();             pause = false;             this.start_button.click -= new system.eventhandler(this.resume_button_click);         }         start = false; stop = true;     }      private int activethreads = 0;     private thread thread;     private void dowork(object sender)     {         string line = null;         ereader = new streamreader(my_list);                 {             lock (ereader)             {                 pauseevent.waitone();                 line = ereader.readline();             }              //             //other commands processing & building argument             //              lock (signal)             {                 ++activethreads;             }              thread = new thread(new parameterizedthreadstart(                 o =>                 {                     processit((object)o);                     lock (signal)                     {                         --activethreads;                         monitor.pulse(signal);                     }                 }));             thread.start(argument);              lock (signal)             {                 while (activethreads > maxthreads)                     monitor.wait(signal);             }              lock (signal)             {                 if (!start)                 {                     showwaiting(true);//shows animated gif "please wait" msg                     while (activethreads > 0)                         monitor.wait(signal);                     showwaiting(false);                     if (stop == true)                     {                         this._backgroundworker.cancelasync();                         break;                     }                 }             }         }         while (ereader.peek() != -1);         showwaiting(true);         lock (signal)         {             while (activethreads > 0)                 monitor.wait(signal);         }         showwaiting(false);     }       private void _backgroundworker_runworkercompleted(object sender, runworkercompletedeventargs e)     {         start = false; stop = true;         guiupdate();     } 

what can avoid duplicate commands inside , outside loop ?

mentioned in question comments suggestions code practice changes.

  • do not use == true or == false when checking boolean variable in condition. (jeff)
  • use enumerate represent state instead of multiple boolean variables.

i add:

  • use proper c# naming conventions. methods if there multiple words, capitalize words. (e.g. instead of showwaiting use showwaiting).

for small loops, don't need optimize them out though there more simple loop. refactor them separate method along code same:

private void showandwait() {     showwaiting(true);     lock (signal)     {         while (activethreads > 0)             monitor.wait(signal);     }     showwaiting(false); } 

note fine same thread lock on monitor acquired. fast operation since thread owns lock. can call method both places.


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -