【问题标题】:How do i change label in another form with backgroundworker process c#?如何使用 backgroundworker 进程 c# 以另一种形式更改标签?
【发布时间】:2019-02-14 03:00:35
【问题描述】:

我的项目 c# 中有 2 个表单(formA 和 formB),当我单击 formA 中的按钮时,我想在 backgroundworker 中运行一些进程。

我可以从后台工作人员更新为 formB 中的标签吗? 这是formA中的代码

        private void button1_Click_1(object sender, EventArgs e)
    {
        backgroundWorker1.RunWorkerAsync();
    }

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        Stimulus stimulus = new Stimulus();
        Stopwatch watch = new Stopwatch();
        stimulus.Show();
        stimulus.Invoke((MethodInvoker)delegate { stimulus.perbaharuiStimulus("+"); });
        watch.Start();
        do
        {

        } while (watch.Elapsed.Seconds != 2);
        watch.Restart();
        stimulus.Invoke((MethodInvoker)delegate { stimulus.perbaharuiStimulus("MAJU"); });
        do
        {

        } while (watch.Elapsed.Seconds != 6);
        watch.Restart();
        stimulus.Invoke((MethodInvoker)delegate { stimulus.perbaharuiStimulus(""); });
        do
        {

        } while (watch.Elapsed.Seconds != 2);
        watch.Stop();
        stimulus.Close();
    }

这是表单B中的代码

        public Stimulus()
    {
        InitializeComponent();
        FormBorderStyle = FormBorderStyle.None;
        WindowState = FormWindowState.Maximized;
    }
    public void perbaharuiStimulus(string stimulus)
    {
        this.Invoke((MethodInvoker)delegate
        {
            lbStimulus.Text = stimulus;
        });
    }

感谢您的关注..

【问题讨论】:

标签: c# multithreading backgroundworker


【解决方案1】:

您可以像下面这样更改您的代码,它会正常工作。

  1. 将 perbaharuiStimulus 代码更改为

    lbStimulus.Text = stimulus;
    
  2. WorkerReportsProgress更改为True

  3. backgroundWorker1_DoWork改成下面

        Stimulus stimulus = new Stimulus();
        Stopwatch watch = new Stopwatch();
        backgroundWorker1.ReportProgress(1, stimulus);
        watch.Start();
        do
        {
    
        } while (watch.Elapsed.Seconds != 2);
    
        watch.Restart();
        backgroundWorker1.ReportProgress(2, stimulus);
        do
        {
    
        } while (watch.Elapsed.Seconds != 6);
    
        watch.Restart();
        backgroundWorker1.ReportProgress(3, stimulus);
        do
        {
    
        } while (watch.Elapsed.Seconds != 2);
        watch.Stop();
    
        stimulus.Invoke((MethodInvoker)delegate { stimulus.Close(); });
    
  4. 添加backgroundWorker1_ProgressChanged 事件并将以下代码放入其中

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        Stimulus stimulus = ( Stimulus)e.UserState;
        if(e.ProgressPercentage==1)
            stimulus.perbaharuiStimulus("+");
        if (e.ProgressPercentage == 2)
            stimulus.perbaharuiStimulus("MAJU");
        if (e.ProgressPercentage == 3)
            stimulus.perbaharuiStimulus("");
    
        stimulus.Show();
    }
    

希望对你有帮助!

【讨论】:

  • 欢迎来到 Stack Overflow!您已经写了一个很好的答案,但是您真的不应该在后台工作人员中实例化 Stimulus 表单,因为这样做会将其绑定到后台线程而不是 UI 线程。您想在 DoWork 之前或在其中的 Invoke() 调用中实例化表单。否则干得好!
【解决方案2】:

您不应该在后台线程中创建表单。这样做会将表单分配给 that 线程而不是 UI 线程,这意味着该表单现在位于与消息泵不同的线程上。

解决此问题的方法是在 UI 线程上调用表单的实例化和查看,然后您的以下 Invoke 调用应该可以工作。

Stimulus stimulus;

this.Invoke((MethodInvoker)delegate
{
    stimulus = new Stimulus();
    stimulus.Show();
});

//No need to invoke this since perbaharuiStimulus() calls Invoke() as well.
stimulus.perbaharuiStimulus("+");

//The rest of your code...

除此之外,您所做的一切都是正确的。

【讨论】:

    【解决方案3】:

    您可以使用Invoke(...) 从后台工作人员更新任何形式的标签,就像您之前所做的那样。 (假设刺激是一个场)。 调用一次 Invoke 就足够了。 Stimulus.Invoke 在激励表单的控制线程上执行委托。所以你可以决定,当你调度线程时。我建议在 perbarauiStimulus 中执行此操作,因为这样可以减少有人忘记发送呼叫的机会。

    但是您的代码存在一个潜在问题:

    不要使用经过时间的精确比较。更喜欢使用'>='。由于您处理的是秒数,因此这很少会成为实际问题,但可能会导致无限循环。

    如果stimulus不是一个字段,你必须在后台工作人员外部创建一个Stimulus的实例,因为如果你在worker方法内部创建它,表单将运行它的后台工作线程上的消息循环。这消除了后台工作人员的使用,因为该操作现在从 sytimulus 视图同步运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-08-15
      • 1970-01-01
      • 1970-01-01
      • 2021-04-22
      • 2012-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多