c# BackgroundWorker组件的作用
程序员文章站
2022-09-02 11:22:25
当构建一个图形化的windows form桌面应用程序并且需要执行在应用程序主ui线程之外的线程中长时间的任务时,backgroundworker类就很有用了。要使用backgroundworker,...
当构建一个图形化的windows form桌面应用程序并且需要执行在应用程序主ui线程之外的线程中长时间的任务时,backgroundworker类就很有用了。
要使用backgroundworker,我们只需要告诉它希望在后台执行那个方法并且调用runworkerasync()即可
public partial class form1 : form { public form1() { initializecomponent(); } private void button1_click(object sender, eventargs e) { int a = int.parse(textbox1.text.trim()); int b = int.parse(textbox2.text.trim()); add ad = new add(a,b); backgroundworker1.runworkerasync(ad); } private void backgroundworker1_dowork(object sender, doworkeventargs e) { add args=(add)e.argument; for (int i = 0; i < 11; i++) { thread.sleep(200); backgroundworker1.reportprogress(i*10); } e.result = args.a + args.b; } private void backgroundworker1_runworkercompleted(object sender, runworkercompletedeventargs e) { label1.text = e.result.tostring(); } private void backgroundworker1_progresschanged(object sender, progresschangedeventargs e) { progressbar1.value = e.progresspercentage; } } public class add { public int a; public int b; public add(int a, int b) { this.a = a; this.b = b; } }
以上就是c# backgroundworker组件的作用的详细内容,更多关于c# backgroundworker组件的资料请关注其它相关文章!
上一篇: 详解webpack异步加载业务模块