C# 无法使用后台辅助角色来使用 while 循环

本文关键字:角色 循环 while 后台 | 更新日期: 2023-09-27 18:35:08

我使用的是 C# 表单,我本质上想为当前时间的每一秒更新一个标签。显然,UI 线程妨碍了我和东西,所以我求助于后台工作人员,我坐在这里挠头,试图每秒更新我的标签。我尝试在多个位置使用 while 循环,但它不起作用。我显然做错了什么...有什么解决办法吗?

法典:

MainForm.Designer.cs:

using System;
namespace alarmClock
{
partial class MainForm
{
    System.ComponentModel.IContainer components = null;
    protected override void Dispose(bool disposing)
    {
        if (disposing) {
            if (components != null) {
                components.Dispose();
            }
        }
        base.Dispose(disposing);
    }
    public static DateTime now = DateTime.Now;
    public static string time = now.ToLongTimeString();
    public void setRealHour() {
        hour.Text = time[0].ToString() + time[1].ToString();
    }
    public void setRealMinute() {
        minute.Text = time[3].ToString() + time[4].ToString();
    }
    public void setRealSecond() {
        second.Text = time[6].ToString() + time[7].ToString();
    }
    public void setHourTime() {
        setHour.Text = MainForm.setH.ToString();
        if(MainForm.setH < 10) {
            setHour.Text = "0" + MainForm.setH.ToString();
        }
        if(MainForm.setH > 24) {
            MainForm.setH=0;
            setHour.Text = "0" + MainForm.setH.ToString();
        }
    }
    public void setMinuteTime() {
        setMinute.Text = MainForm.setM.ToString();
        if(MainForm.setM < 10) {
            setMinute.Text = "0" + MainForm.setM.ToString();
        }
        if(MainForm.setM > 60) {
            MainForm.setM=0;
            setMinute.Text = "0" + MainForm.setM.ToString();
        }
    }
    public void setSecondTime() {
        setSecond.Text = MainForm.setS.ToString();
        if(MainForm.setS < 10) {
            setSecond.Text = "0" + MainForm.setS.ToString();
        }
        if(MainForm.setS > 60) {
            MainForm.setS=0;
            setSecond.Text = "0" + MainForm.setS.ToString();
        }
    }
    private void InitializeComponent()
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
        this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
        this.minute = new System.Windows.Forms.Label();
        this.hour = new System.Windows.Forms.Label();
        this.second = new System.Windows.Forms.Label();
        this.checkboxTime = new System.Windows.Forms.CheckedListBox();
        this.delTime = new System.Windows.Forms.Button();
        this.tableLayoutPanel2 = new System.Windows.Forms.TableLayoutPanel();
        this.tableLayoutPanel3 = new System.Windows.Forms.TableLayoutPanel();
        this.setHour = new System.Windows.Forms.Label();
        this.setMinute = new System.Windows.Forms.Label();
        this.setSecond = new System.Windows.Forms.Label();
        this.tableLayoutPanel4 = new System.Windows.Forms.TableLayoutPanel();
        this.subHour = new System.Windows.Forms.Button();
        this.addHour = new System.Windows.Forms.Button();
        this.subMin = new System.Windows.Forms.Button();
        this.addMin = new System.Windows.Forms.Button();
        this.subSec = new System.Windows.Forms.Button();
        this.addSec = new System.Windows.Forms.Button();
        this.setTime = new System.Windows.Forms.GroupBox();
        this.addTime = new System.Windows.Forms.Button();
        this.bgwM = new System.ComponentModel.BackgroundWorker();
        this.bgwS = new System.ComponentModel.BackgroundWorker();
        this.bgwH = new System.ComponentModel.BackgroundWorker();
        this.tableLayoutPanel1.SuspendLayout();
        this.tableLayoutPanel2.SuspendLayout();
        this.tableLayoutPanel3.SuspendLayout();
        this.tableLayoutPanel4.SuspendLayout();
        this.setTime.SuspendLayout();
        this.SuspendLayout();
        // 
        // tableLayoutPanel1
        // 
        resources.ApplyResources(this.tableLayoutPanel1, "tableLayoutPanel1");
        this.tableLayoutPanel1.Controls.Add(this.minute, 1, 0);
        this.tableLayoutPanel1.Controls.Add(this.hour, 0, 0);
        this.tableLayoutPanel1.Controls.Add(this.second, 2, 0);
        this.tableLayoutPanel1.Name = "tableLayoutPanel1";
        // 
        // minute
        // 
        resources.ApplyResources(this.minute, "minute");
        this.minute.ForeColor = System.Drawing.Color.Black;
        this.minute.Name = "minute";
        // 
        // hour
        // 
        resources.ApplyResources(this.hour, "hour");
        this.hour.ForeColor = System.Drawing.Color.Black;
        this.hour.Name = "hour";
        // 
        // second
        // 
        resources.ApplyResources(this.second, "second");
        this.second.ForeColor = System.Drawing.Color.Black;
        this.second.Name = "second";
        // 
        // checkboxTime
        // 
        this.checkboxTime.BackColor = System.Drawing.Color.Silver;
        resources.ApplyResources(this.checkboxTime, "checkboxTime");
        this.checkboxTime.ForeColor = System.Drawing.Color.Black;
        this.checkboxTime.FormattingEnabled = true;
        this.checkboxTime.Items.AddRange(new object[] {
                                resources.GetString("checkboxTime.Items"),
                                resources.GetString("checkboxTime.Items1")});
        this.checkboxTime.Name = "checkboxTime";
        this.checkboxTime.SelectedIndexChanged += new System.EventHandler(this.CheckboxTimeSelectedIndexChanged);
        // 
        // delTime
        // 
        resources.ApplyResources(this.delTime, "delTime");
        this.delTime.Name = "delTime";
        this.delTime.UseVisualStyleBackColor = true;
        this.delTime.Click += new System.EventHandler(this.DelTimeClick);
        // 
        // tableLayoutPanel2
        // 
        resources.ApplyResources(this.tableLayoutPanel2, "tableLayoutPanel2");
        this.tableLayoutPanel2.Controls.Add(this.tableLayoutPanel3, 0, 1);
        this.tableLayoutPanel2.Controls.Add(this.tableLayoutPanel4, 0, 0);
        this.tableLayoutPanel2.Name = "tableLayoutPanel2";
        // 
        // tableLayoutPanel3
        // 
        resources.ApplyResources(this.tableLayoutPanel3, "tableLayoutPanel3");
        this.tableLayoutPanel3.Controls.Add(this.setHour, 0, 0);
        this.tableLayoutPanel3.Controls.Add(this.setMinute, 1, 0);
        this.tableLayoutPanel3.Controls.Add(this.setSecond, 2, 0);
        this.tableLayoutPanel3.Name = "tableLayoutPanel3";
        // 
        // setHour
        // 
        resources.ApplyResources(this.setHour, "setHour");
        this.setHour.ForeColor = System.Drawing.Color.Black;
        this.setHour.Name = "setHour";
        // 
        // setMinute
        // 
        resources.ApplyResources(this.setMinute, "setMinute");
        this.setMinute.ForeColor = System.Drawing.Color.Black;
        this.setMinute.Name = "setMinute";
        // 
        // setSecond
        // 
        resources.ApplyResources(this.setSecond, "setSecond");
        this.setSecond.ForeColor = System.Drawing.Color.Black;
        this.setSecond.Name = "setSecond";
        // 
        // tableLayoutPanel4
        // 
        resources.ApplyResources(this.tableLayoutPanel4, "tableLayoutPanel4");
        this.tableLayoutPanel4.Controls.Add(this.subHour, 0, 0);
        this.tableLayoutPanel4.Controls.Add(this.addHour, 1, 0);
        this.tableLayoutPanel4.Controls.Add(this.subMin, 2, 0);
        this.tableLayoutPanel4.Controls.Add(this.addMin, 3, 0);
        this.tableLayoutPanel4.Controls.Add(this.subSec, 4, 0);
        this.tableLayoutPanel4.Controls.Add(this.addSec, 5, 0);
        this.tableLayoutPanel4.Name = "tableLayoutPanel4";
        // 
        // subHour
        // 
        resources.ApplyResources(this.subHour, "subHour");
        this.subHour.Name = "subHour";
        this.subHour.UseVisualStyleBackColor = true;
        this.subHour.Click += new System.EventHandler(this.SubHourClick);
        // 
        // addHour
        // 
        resources.ApplyResources(this.addHour, "addHour");
        this.addHour.Name = "addHour";
        this.addHour.UseVisualStyleBackColor = true;
        this.addHour.Click += new System.EventHandler(this.AddHourClick);
        // 
        // subMin
        // 
        resources.ApplyResources(this.subMin, "subMin");
        this.subMin.Name = "subMin";
        this.subMin.UseVisualStyleBackColor = true;
        this.subMin.Click += new System.EventHandler(this.SubMinClick);
        // 
        // addMin
        // 
        resources.ApplyResources(this.addMin, "addMin");
        this.addMin.Name = "addMin";
        this.addMin.UseVisualStyleBackColor = true;
        this.addMin.Click += new System.EventHandler(this.AddMinClick);
        // 
        // subSec
        // 
        resources.ApplyResources(this.subSec, "subSec");
        this.subSec.Name = "subSec";
        this.subSec.UseVisualStyleBackColor = true;
        this.subSec.Click += new System.EventHandler(this.SubSecClick);
        // 
        // addSec
        // 
        resources.ApplyResources(this.addSec, "addSec");
        this.addSec.Name = "addSec";
        this.addSec.UseVisualStyleBackColor = true;
        this.addSec.Click += new System.EventHandler(this.AddSecClick);
        // 
        // setTime
        // 
        resources.ApplyResources(this.setTime, "setTime");
        this.setTime.BackColor = System.Drawing.Color.Silver;
        this.setTime.Controls.Add(this.addTime);
        this.setTime.Controls.Add(this.tableLayoutPanel2);
        this.setTime.Name = "setTime";
        this.setTime.TabStop = false;
        // 
        // addTime
        // 
        resources.ApplyResources(this.addTime, "addTime");
        this.addTime.Name = "addTime";
        this.addTime.UseVisualStyleBackColor = true;
        this.addTime.Click += new System.EventHandler(this.AddTimeClick);
        // 
        // bgwM
        // 
        this.bgwM.DoWork += new System.ComponentModel.DoWorkEventHandler(this.BgwMDoWork);
        // 
        // bgwS
        // 
        this.bgwS.DoWork += new System.ComponentModel.DoWorkEventHandler(this.BgwSDoWork);
        // 
        // bgwH
        // 
        this.bgwH.DoWork += new System.ComponentModel.DoWorkEventHandler(this.BgwHDoWork);
        // 
        // MainForm
        // 
        resources.ApplyResources(this, "$this");
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.BackColor = System.Drawing.Color.DarkGray;
        this.Controls.Add(this.setTime);
        this.Controls.Add(this.delTime);
        this.Controls.Add(this.checkboxTime);
        this.Controls.Add(this.tableLayoutPanel1);
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
        this.Name = "MainForm";
        this.tableLayoutPanel1.ResumeLayout(false);
        this.tableLayoutPanel1.PerformLayout();
        this.tableLayoutPanel2.ResumeLayout(false);
        this.tableLayoutPanel2.PerformLayout();
        this.tableLayoutPanel3.ResumeLayout(false);
        this.tableLayoutPanel3.PerformLayout();
        this.tableLayoutPanel4.ResumeLayout(false);
        this.setTime.ResumeLayout(false);
        this.setTime.PerformLayout();
        this.ResumeLayout(false);
        this.PerformLayout();
    }
    private System.ComponentModel.BackgroundWorker bgwH;
    private System.ComponentModel.BackgroundWorker bgwS;
    private System.ComponentModel.BackgroundWorker bgwM;
    System.Windows.Forms.Button addTime;
    System.Windows.Forms.GroupBox setTime;
    System.Windows.Forms.Button addSec;
    System.Windows.Forms.Button subSec;
    System.Windows.Forms.Button addMin;
    System.Windows.Forms.Button subMin;
    System.Windows.Forms.Button addHour;
    System.Windows.Forms.TableLayoutPanel tableLayoutPanel4;
    System.Windows.Forms.Label setSecond;
    System.Windows.Forms.Label setMinute;
    System.Windows.Forms.Label setHour;
    System.Windows.Forms.TableLayoutPanel tableLayoutPanel3;
    System.Windows.Forms.Button subHour;
    System.Windows.Forms.TableLayoutPanel tableLayoutPanel2;
    System.Windows.Forms.Button delTime;
    System.Windows.Forms.CheckedListBox checkboxTime;
    System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
    System.Windows.Forms.Label second;
    System.Windows.Forms.Label hour;
    System.Windows.Forms.Label minute;
    void BgwHDoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
    {
        setRealHour();
    }
    void BgwMDoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
    {
        setRealMinute();
    }
    void BgwSDoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
    {
        setRealSecond();
    }
}
}

主窗体.cs:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace alarmClock
{
public partial class MainForm : Form
{
    public static int setH = 0;
    public static int setM = 0;
    public static int setS = 0;
    public MainForm()
    {
        InitializeComponent();
        bgwH.RunWorkerAsync();
        bgwM.RunWorkerAsync();
        bgwS.RunWorkerAsync();
    }
    void DelTimeClick(object sender, EventArgs e)
    {
    }
    void SubHourClick(object sender, EventArgs e)
    {
        setH--;
        setHourTime();
    }
    void AddHourClick(object sender, EventArgs e)
    {
        setH++;
        setHourTime();
    }
    void SubMinClick(object sender, EventArgs e)
    {
        setM--;
        setMinuteTime();
    }
    void AddMinClick(object sender, EventArgs e)
    {
        setM++;
        setMinuteTime();
    }
    void SubSecClick(object sender, EventArgs e)
    {
        setS--;
        setSecondTime();
    }
    void AddSecClick(object sender, EventArgs e)
    {
        setS++;
        setSecondTime();
    }
    void AddTimeClick(object sender, EventArgs e)
    {
    }

    void CheckboxTimeSelectedIndexChanged(object sender, EventArgs e)
    {
    }
}
}

C# 无法使用后台辅助角色来使用 while 循环

正如杰里米在评论中指出的那样,Timer会是更好的选择。

为了完整起见,如果您仍然想使用 BackgroundWorker ,由于其他原因,您将需要使用BackgroundWorker.ReportProgress()方法。 任何 UI 工作都必须在主 UI 线程上完成。 使用 BackgroundWorker 时,您需要使用 Reporting 事件处理程序在主线程上获取回调。

您需要确保BackgroundWorker.WorkerReportsProgress设置为 TRUE。 这不是默认的。 然后将标签集方法移动到事件处理程序中ProgressChanged