Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted

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


C#
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;
}




Regards
Dominic
 
Share this answer
 
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:

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




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

Regards
Dominic
 
Share this answer
 
Comments
Dustin Prevatt 4-Dec-12 12:15pm    
I am using the backgroundworker because I am copying files that takes a while in my button click, so I would like the label to flash as soon as the button is pressed not after it is finished doing all that it is doing. As I said, i'm pretty new at this so what is this ((MethodInvoker)?
Dominic Abraham 4-Dec-12 12:46pm    
HiHere you are trying to access a control in one thread from a different thread. That is timer is part of your main thread and backgroundworker is part of the other thread. So there is a cross thread communication happening. If you are trying to change the visibility property of TransferStatusLabel1 from your backgroundWorker1_DoWork event, you will get a "Cross-thread operation not valid error". So to access a control from different thread, we are using Invoke(). As an argument we should specify the delegate. MethodeInvoker is a delegate which allows us to call a function which has no arguments. So we are specifying this.invoke((MethodInvoker)delegate()). There are good articles giving detailed information about this.
http://www.codeproject.com/Articles/14931/Asynchronous-Method-Invocation
http://msdn.microsoft.com/en-us/library/system.windows.forms.methodinvoker.aspx
http://timl.net/2008/01/begininvoke-methodinvoker-and-anonymous.html
Dominic Abraham 4-Dec-12 12:47pm    
If the solution is helpful don't forget to mark it as answer. So that it may help others with similar issues.
Dustin Prevatt 4-Dec-12 16:49pm    
I'm still confused, I also read that the timer runs on a separate thread so I may not need the backgroundworker. Basically when I click my button I want to flash a label that is coming from my timer_tick. Is it possible for the timer to start on button_click instead of waiting for all the other work to finish?
Dominic Abraham 4-Dec-12 21:21pm    
Hi
As i said you earlier, you don't need the backgroundworker to just blink the label. you can do that timer_tick event itself.
You are using timer control [System.Windows.Forms.Timer], which is running in main application thread itself. There are some other timers which are running in another thread. You just read that in web.
http://www.it-sideways.com/2010/04/systemtimerstimer-vs.html

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900