C# GUI未使用计时器或BackgroundWorker更新
我试图每秒钟更新一次GUI。我试过System.Timers.Timer、System.Windows.Forms.Timer和BackgroundWorker。没有骰子。我显然错过了什么 我对BackgroundWorker的尝试:C# GUI未使用计时器或BackgroundWorker更新,c#,user-interface,timer,C#,User Interface,Timer,我试图每秒钟更新一次GUI。我试过System.Timers.Timer、System.Windows.Forms.Timer和BackgroundWorker。没有骰子。我显然错过了什么 我对BackgroundWorker的尝试: public partial class Form1 : Form { List<int> counts = new List<int>() { 0, 0, 0, 0, 0, 0, 0, 0 }; List<long&
public partial class Form1 : Form
{
List<int> counts = new List<int>() { 0, 0, 0, 0, 0, 0, 0, 0 };
List<long> complete = new List<long>() { 0, 0, 0, 0, 0, 0, 0, 0 };
private void button1_Click(object sender, EventArgs e)
{
var backgroundWorker = new BackgroundWorker();
backgroundWorker.DoWork += new DoWorkEventHandler(UpdateGUI);
backgroundWorker.RunWorkerAsync();
// Do stuff that updates counts and complete
backgroundWorker.CancelAsync();
}
private void UpdateGUI(object sender, EventArgs e)
{
var backgroundWorker = sender as BackgroundWorker;
while (backgroundWorker.CancellationPending == false)
{
label1.Text = count.ToString();
labelF1.Text = counts[0].ToString();
labelF2.Text = counts[1].ToString();
labelF3.Text = counts[2].ToString();
labelF4.Text = counts[3].ToString();
labelF5.Text = counts[4].ToString();
labelF6.Text = counts[5].ToString();
labelF7.Text = counts[6].ToString();
labelF8.Text = counts[7].ToString();
labelC1.Text = complete[0].ToString();
labelC2.Text = complete[1].ToString();
labelC3.Text = complete[2].ToString();
labelC4.Text = complete[3].ToString();
labelC5.Text = complete[4].ToString();
labelC6.Text = complete[5].ToString();
labelC7.Text = complete[6].ToString();
labelC8.Text = complete[7].ToString();
Application.DoEvents();
Thread.Sleep(1000);
}
}
}
公共部分类表单1:表单
{
列表计数=新列表(){0,0,0,0,0,0,0,0};
List complete=new List(){0,0,0,0,0,0,0};
私有无效按钮1\u单击(对象发送者,事件参数e)
{
var backgroundWorker=新的backgroundWorker();
backgroundWorker.DoWork+=新的DoWorkEventHandler(UpdateGUI);
backgroundWorker.RunWorkerAsync();
//做一些有价值和完整的更新
backgroundWorker.CancelAsync();
}
私有void UpdateGUI(对象发送方,事件参数)
{
var backgroundWorker=发送方作为backgroundWorker;
while(backgroundWorker.CancellationPending==false)
{
label1.Text=count.ToString();
labelF1.Text=计数[0]。ToString();
labelF2.Text=计数[1]。ToString();
labelF3.Text=计数[2]。ToString();
labelF4.Text=计数[3]。ToString();
labelF5.Text=计数[4]。ToString();
labelF6.Text=计数[5]。ToString();
labelF7.Text=计数[6]。ToString();
labelF8.Text=计数[7]。ToString();
labelC1.Text=complete[0].ToString();
labelC2.Text=complete[1].ToString();
labelC3.Text=complete[2].ToString();
labelC4.Text=complete[3].ToString();
labelC5.Text=complete[4].ToString();
labelC6.Text=complete[5].ToString();
labelC7.Text=complete[6].ToString();
labelC8.Text=complete[7].ToString();
Application.DoEvents();
睡眠(1000);
}
}
}
试试这个
public partial class Form1 : Form
{
List<int> counts = new List<int>() { 0, 0, 0, 0, 0, 0, 0, 0 };
List<long> complete = new List<long>() { 0, 0, 0, 0, 0, 0, 0, 0 };
private void button1_Click(object sender, EventArgs e)
{
var backgroundWorker = new BackgroundWorker();
backgroundWorker.DoWork += new DoWorkEventHandler(UpdateGUI);
backgroundWorker.RunWorkerCompleted += backgroundWorker_RunWorkerCompleted
backgroundWorker.RunWorkerAsync();
// Do stuff that updates counts and complete
//backgroundWorker.CancelAsync();
}
private void UpdateGUI(object sender, EventArgs e)
{
Thread.Sleep(1000);
}
private void backgroundWorker.RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
var backgroundWorker = sender as BackgroundWorker;
label1.Text = count.ToString();
labelF1.Text = counts[0].ToString();
labelF2.Text = counts[1].ToString();
labelF3.Text = counts[2].ToString();
labelF4.Text = counts[3].ToString();
labelF5.Text = counts[4].ToString();
labelF6.Text = counts[5].ToString();
labelF7.Text = counts[6].ToString();
labelF8.Text = counts[7].ToString();
labelC1.Text = complete[0].ToString();
labelC2.Text = complete[1].ToString();
labelC3.Text = complete[2].ToString();
labelC4.Text = complete[3].ToString();
labelC5.Text = complete[4].ToString();
labelC6.Text = complete[5].ToString();
labelC7.Text = complete[6].ToString();
labelC8.Text = complete[7].ToString();
Application.DoEvents();
}
}
公共部分类表单1:表单
{
列表计数=新列表(){0,0,0,0,0,0,0,0};
List complete=new List(){0,0,0,0,0,0,0};
私有无效按钮1\u单击(对象发送者,事件参数e)
{
var backgroundWorker=新的backgroundWorker();
backgroundWorker.DoWork+=新的DoWorkEventHandler(UpdateGUI);
backgroundWorker.RunWorkerCompleted+=backgroundWorker\u RunWorkerCompleted
backgroundWorker.RunWorkerAsync();
//做一些有价值和完整的更新
//backgroundWorker.CancelAsync();
}
私有void UpdateGUI(对象发送方,事件参数)
{
睡眠(1000);
}
私有void backgroundWorker.RunWorkerCompleted(对象发送方,RunWorkerCompletedEventArgs e)
{
var backgroundWorker=发送方作为backgroundWorker;
label1.Text=count.ToString();
labelF1.Text=计数[0]。ToString();
labelF2.Text=计数[1]。ToString();
labelF3.Text=计数[2]。ToString();
labelF4.Text=计数[3]。ToString();
labelF5.Text=计数[4]。ToString();
labelF6.Text=计数[5]。ToString();
labelF7.Text=计数[6]。ToString();
labelF8.Text=计数[7]。ToString();
labelC1.Text=complete[0].ToString();
labelC2.Text=complete[1].ToString();
labelC3.Text=complete[2].ToString();
labelC4.Text=complete[3].ToString();
labelC5.Text=complete[4].ToString();
labelC6.Text=complete[5].ToString();
labelC7.Text=complete[6].ToString();
labelC8.Text=complete[7].ToString();
Application.DoEvents();
}
}
您是否收到任何异常backgroundWorker.CancelAsync()代码>似乎是导致失败的主要原因。此外,您的数据永远不会更改也可能有问题。您也在读写共享状态,而没有考虑锁定。在一天结束时,“不更新”是一个非常糟糕的描述你的问题,所以这里所说的一切只能是猜测。我会坚持使用System.Windows.Forms.Timer,因为这些回调保证会发生在UI线程上<代码>应用程序。DoEvents
保证不是所有应用程序都需要的。如果您需要调用它,那是因为您已经出错了。@spender当我在“执行更新计数并完成的操作”部分中调用UpDateGUI()时,一切都很好。@rajeev没有例外,只是没有显示(甚至不是第一次显示零).在backgroundWorker.RunWorkerCompleted+=backgroundWorker\u RunWorkerCompleted事件中完成您的工作