试图从我的后台工作者开始一个timer_tick [英] Trying to start a timer_tick from my backgroundworker

查看:79
本文介绍了试图从我的后台工作者开始一个timer_tick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用backgroundworker来调用在我的Windows窗体UI中闪烁标签的计时器,但我没有运气。这是我的代码。



I am attempting to use backgroundworker to call a timer that flashes a label in my windows form UI, but I am not having any luck. Here is my code.

private void button1_Click(object sender, EventArgs e)
       {
        backgroundWorker1.RunWorkerAsync();
        //do alot of other work
       }






       private void timer1_Tick(object sender, EventArgs e)
       {
            TransferStatusLabel1.Visible = !TransferStatusLabel1.Visible;
       }

       private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
       {
           BackgroundWorker worker = sender as BackgroundWorker;


           timer1.Start();
           timer1.Enabled = true;

       }

       private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
       {
           MessageBox.Show("done");
           timer1.Start();
       }

    }
   }





背景工作者似乎工作,但它似乎没有启动计时器或显示标签,我不知道我的问题是与背景工作者或计时器?我是新手,所以请帮忙。

.

The backgroundworker seems to be working but it doesn''t seem to start the timer or show the label, I''m not sure if my issue is with the backgroundworker or the timer? I am pretty new to this so please help.

推荐答案

你好b $ b

如果你只是想让标签闪烁,我认为以下代码就够了:





Hi
If you just want to blink the label, i think the below code is enough:


private void btnStartTimer_Click(object sender, EventArgs e)
{
    timer1.Enabled = true;
    timer1.Interval = 500;
    timer1.Start();
}

private void btnStopTimer_Click(object sender, EventArgs e)
{
    timer1.Stop();
}
private void timer1_Tick(object sender, EventArgs e)
{
    TransferStatusLabel1.Visible = !TransferStatusLabel1.Visible;
}









问候

Dominic





Regards
Dominic


您好



Timer和Backgroundworker在两个不同的线程中运行。所以你不能简单地从Backgroundworker访问定时器控件。你可以试试下面的代码:



Hi

Timer and Backgroundworker are running in two different threads. So you cannot simply access the timer control from Backgroundworker. Can you please try the below code:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
     {
         this.Invoke((MethodInvoker)delegate()
         {
             timer1.Enabled = true;
             timer1.Start();
         });
     }









注意:如果您的需求是只显示闪烁的标签,定时器控制就足够了。没有背景工作者。



问候

Dominic





NB: If your need is to just display blinking label, timer control is enough. There is no need of backgroundworker.

Regards
Dominic


这篇关于试图从我的后台工作者开始一个timer_tick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆