c# - Winform how to deal with method freezing UI -
i have method creates string variables expanded more modes, , sends string down serial port. serial port writing enabled freezes ui, without it, runs fine.
private void ontimedevent(object myobject, eventargs myeventargs) { l = (l > 100) ? 100 : l; r = (r > 100) ? 100 : r; mrectangle1 = new rectanglef(13, 153, 80 * (l / 100), 16); mrectangle2 = new rectanglef(173 - (80 * (r / 100)), 153, 80 * (r / 100), 16); string start = ""; (int = 0; < 25; i++) { start += "0:" + math.round(255 * (l / 100)) + ":0;"; } start += "."; port.write(start); invalidate(); }
how best deal this, can send down serial port without freezing ui?
you can use backgroundworker
class this.
the dowork
event potentially time consuming work done on background thread. can pass start
argument runworkerasync(object)
method fires dowork
event.
quick example:
// start work in background thread backgroundworker1.runworkerasync(start); // handles time-consuming task without blocking ui thread private void backgroundworker1_dowork(object sender, doworkeventargs e) { string start = (string) e.argument; port.write(start); }
Comments
Post a Comment