serial port - Infinite loop c# checkbox -


this may stupid question because new c# trying send signal using serial port in single send mode, , continuous mode. have check box indicate send mode. when click on check box , click send, application gets stuck in infinite loop , lose control of application.

    private void bsend_click(object sender, eventargs e)     {         byte[] break = new byte[1] { 0x00 };         string input = tbtx.text;         byte[] bytestosend = input.split().select(s => convert.tobyte(s, 16)).toarray();         while (cbcontinuous.checked) // checkbox named continuous             {                 // generate break , mab signal                 headersignal();                  // set dmx timing                 dmxsettings();                 myserialport.write(bytestosend, 0, bytestosend.length);              }         // generate break , mab signal         headersignal();          // set dmx timing         dmxsettings();         myserialport.write(bytestosend, 0, bytestosend.length);      } 

is there way can un-check check box while in infinite loop regain control , stop sending? or there better way can this?

one solution use tpl (task parallel library). example of how can this, based on code given in question follows:

    private readonly list<task> tasklist = new list<task>();      private void bsend_click(object sender, eventargs e)     {         var task = task.run(() =>         {             byte[] break = new byte[1] {0x00};             string input = tbtx.text;             byte[] bytestosend = input.split().select(s => convert.tobyte(s, 16)).toarray();             while (cbcontinuous.checked) // checkbox named continuous             {                 // generate break , mab signal                 headersignal();                  // set dmx timing                 dmxsettings();                 myserialport.write(bytestosend, 0, bytestosend.length);              }             // generate break , mab signal             headersignal();              // set dmx timing             dmxsettings();             myserialport.write(bytestosend, 0, bytestosend.length);         });          tasklist.add(task);     } 

you may wish disable elements of gui while running, prevent subsequent button clicks running task again @ same time.

you may wish read question on stack overflow before using application.doevents:

use of application.doevents

you may wish consider .net 4.5 has support asynchronous programming via await , async keywords:

asynchronous programming async , await (c# , visual basic)


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -