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:
you may wish consider .net 4.5 has support asynchronous programming via await , async keywords:
Comments
Post a Comment