dcsimg
Results 1 to 4 of 4

Thread: BackGround Worker vs Threading for setting a label color during timer tick event

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2017
    Posts
    9

    BackGround Worker vs Threading for setting a label color during timer tick event

    Hello Guys,
    I have been reading post by NIYA and jmcilhinney concerning Threading and Background Workers. I have even followed along with some examples trying to move towards understanding this aspect of coding. I was going to see about getting some help applying these things to my code that I have.

    My first question is how can I change a label color while my timer event is going through its routine:
    Code:
    Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
    
            GetlvIncNumbers()
            CheckIncNumber()
            If ListView1.SelectedItems.Count > 0 Then
                GetCallNotes()
            End If
            GetUnitStatus()
            '** Had this turned off for some reason, figure out what issues it was causing
            GetSelectedCallDetails()
            'Display Update Time
            lblUpdateTime.Text = ""
            lblUpdateTime.Text = ("Last Updated: " & Date.Now.ToString("dddd HH:mm:ss"))
             lblUpdateTime.BackColor = Color.Red
        End Sub
    Basically the timer event calls several subs that get data from a database, this generally is not an issue because it happens rather quickly since the amount of data is relatively small, but if there is heavy network traffic or a not so go good data then it can get slow. So I want my label back color to turn green while the event fires. So would a Background Worker or using Threading work better and how would I apply it to my example.

    After I get this down I want to move into using threading for my SQL controls, but I'll start small and get going from there. Thank you all for the help.

    Matt Griffin
    VS2010

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,586

    Re: BackGround Worker vs Threading for setting a label color during timer tick event

    The Tick event of a WinForms Timer is raised on the UI thread, so your event handler will be executed on the UI thread. That means that nothing else can be done on the UI thread while that's happening. You can change the BackColor of a Label in there but you won't actually see that change until the event handler completes because the UI thread isn't available to refresh what's on screen.

    One option would be to call the Refresh method of the Label after setting the BackColor, which will force an immediate repaint. Your UI will still remain unresponsive but at least the user will see the colour change in the Label.

    If you want the UI to actually remain responsive then you will need to do the time-consuming work on a secondary thread. One option would be to use a System Timer instead of a WinForms Timer, as it will raise its Elapsed event on a secondary thread by default. That would mean that you'd have to marshal a call back to the UI thread in order to modify the Label though. Alternatively, you could stick with the Timer you have and then start a BackgroundWorker from your Tick event handler. You could modify the Label first, given that you are already on the UI thread, then call RunWorkerAsync. You do the time-consuming work in the DoWork event handler because it's executed on a secondary thread, then you reset the Label in the RunWorkerCompleted event handler because it's executed on the UI thread. Any other changes to the UI, e.g. displaying any data that was retrieved, would also be done in the RunWorkerCompleted event handler.

    You could also look into use Async/Await. Personally, I think that having a reasonable understanding of multi-threading first is an advantage to learning the new asyc pattern, although others may disagree.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2017
    Posts
    9

    Re: BackGround Worker vs Threading for setting a label color during timer tick event

    jmcilhinney,
    Got back to this, has some ice and snow to deal with here. Anyhow because each sub in that timer event reports back to the main UI I would get the cross thread exception. Is it proper to have each sub run in its on background worker report back then move to the next or is there a better alternative? Thank you for your help.

    Matt Griffin
    VS2010

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,586

    Re: BackGround Worker vs Threading for setting a label color during timer tick event

    If those other methods modify the UI too then they would need to be rewritten in order to work in a multi-threaded scenario. That's why it can be a good idea to write functions that are responsible for getting data only and then update the UI with that data separately.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width