Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# BackgroundWorker
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 4-Dec-12 5:24am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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

 
Regards
Dominic
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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

 
NB: If your need is to just display blinking label, timer control is enough. There is no need of backgroundworker.
 
Regards
Dominic
  Permalink  
Comments
Dustin Prevatt at 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 at 4-Dec-12 12:46pm
   
Hi
Here 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 at 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 at 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 at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 352
1 OriginalGriff 344
2 Abdul Samad KP 145
3 CPallini 140
4 Richard MacCutchan 110
0 OriginalGriff 6,249
1 Sergey Alexandrovich Kryukov 5,680
2 CPallini 4,850
3 George Jonsson 3,454
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 4 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100