本文介绍了GUI 未使用计时器或 BackgroundWorker 更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试每秒更新一次 GUI.我尝试过 System.Timers.Timer、System.Windows.Forms.Timer 和 BackgroundWorker.没有骰子.我显然错过了一些东西.

I am attempting to update a GUI every second. I have tried System.Timers.Timer, System.Windows.Forms.Timer, and BackgroundWorker. No dice. I'm obviously missing something.

我对 BackgroundWorker 的尝试:

My attempt with BackgroundWorker:

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);
        }
    }
}

推荐答案

试试这个

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();

   }
 }

这篇关于GUI 未使用计时器或 BackgroundWorker 更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 03:58