使用backgroungworker组件时,dowork会执行两次

原因是事件多次绑定。
designer中的backgroungworker组件

// 
// backgroundWorker1
// 
this.backgroundWorker1.WorkerReportsProgress = true;
this.backgroundWorker1.WorkerSupportsCancellation = true;
this.backgroundWorker1.DoWork += new System.ComponentModel.DoWorkEventHandler(this.backgroundWorker1_DoWork);
1
2
3
4
5
6
窗体代码

public Form1()
        {
            InitializeComponent();
            this.MaximizeBox = false;   //禁用最大化
            form1 = this;
            this.backgroundWorker1.DoWork += backgroundWorker1_DoWork;//工作线程回调,将要执行的代码放在此函数里
            this.backgroundWorker1.ProgressChanged += backgroundWorker1_ProgressChanged;//当进度改变时回调
            this.backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(this.backgroundWorker1_RunWorkerCompleted);//当完成时回调
        }
1
2
3
4
5
6
7
8
9
两段代码中都对DoWork进行了绑定,所以在最后执行时,DoWork会执行两次。我的程序中,造成的结果是同样的弹窗会跳出两次。
 

猜你喜欢

转载自blog.csdn.net/tmjianjun/article/details/86814570
今日推荐