C#多个委托调用多个方法(优先级?)
定义了2个委托,一个用线程启动,一个用backgroundworker启动,为什么我用线程启动的委托刚开始只执行了一遍,等到用backgroundworker启动的委托执行完成后才会继续执行线程的委托?这个难道还有什么优先级?
private int i = 0;private delegate void InvokeLabel();private delegate void InvokeMainForm();private void Form_Load(object sender, EventArgs e){ Thread setLabelTextThread = new Thread(setLabelText); setLabelTextThread.Start(); this.backgroundWorker.RunWorkerAsync();}private void setLabelText(){ InvokeLabel il = delegate(){ this.lable1.Text = i++.toString(); }; this.Invoke(il);}private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e){ while(true){ InvokeMainForm imf = delegate(){ mainForm = new MainForm(); }; this.Invoke(imf); } }
this.Invoke改成this.BeginInvoke
[解决办法]