Stefan Bischof
Stefan Bischof

Reputation: 175

C# Backgroundworker: What way to periodically update a PictureBox?

I'm using a BackgroundWorker to periodically check a hardware switch. Due to it is connected via a slow RS485 network, I have to delay the next status update. On switch Status change I want to update an OK/nOK Picture Box. This is realized as a green OK pictureBox over a nOK pictureBox. No real work is done here.

For expandability I decided to use the Backgroundworker. Finally I want to have a hidden worker, which

  1. provides globally the Status of three switches and
  2. updates on StatusChange the PictureBoxes.

Problem description Once the BackgroundWorker is started, it works as expected. However the GUI freezes.

What did I try? The MSDN BackgroundWorker Class Note 1 says, that GUI should be updated via ProgressChanged. I tried to raise this Event by Worker_Switch.ReportProgress(fakeProgress++) and failed. The PictureBox wasn't updated anymore.

Snippet from designer

this.Worker_Switch = new System.ComponentModel.BackgroundWorker();
// 
// Worker_Switch
// 
this.Worker_Switch.WorkerSupportsCancellation = true;
this.Worker_Switch.DoWork += new System.ComponentModel.DoWorkEventHandler(this.Worker_Switch_DoWork);

Snippet from Main Form

delegate void SetEventCallback(object sender, DoWorkEventArgs e);   // Threadsafe calls for DoWork

private void btnBackgroundworker_Click(object sender, EventArgs e)
{
    if (!Worker_Switch.IsBusy)
    {
        Worker_Switch.RunWorkerAsync();                              
    }
}

private void Worker_Switch_DoWork(object sender, DoWorkEventArgs e)
{
    // Worker Thread has no permission to change PictureBox "pictureBoxSwitchrightOK"
        // Therefore this method calls itsself in the MainThread, if necessary.
    while (!Worker_Switch.CancellationPending)
    {
      if (this.pictureBoxSwitchrightOK.InvokeRequired)              // Worker Thread
    {
            System.Threading.Thread.Sleep(400);
        SetEventCallback myCall = new SetEventCallback(Worker_Switch_DoWork);
        this.Invoke(myCall, new object[] { sender, e });
    }
    else                                                            // Main Thread
    {
        // Turns OK Picture Box invisible, if nOk State (Switch pushed)
        pictureBoxSwitchrightOK.Visible = SwitchOK("right");  // true: OK (green)
        this.Refresh();

    }
}

private bool SwitchOK(string rightOrLeft)               // select one of the switches
{ (...)}                                    // gets hardware switch status

Edit: Special Thanks to laszlokiss88 (3 possibilities) and JMK (for simplicity with System.Windows.Forms Timer from toolbox)

This alternative from Toolbox also worked:

this.timer_Switch.Enabled = true;
    this.timer_Switch.Interval = 400;
    this.timer_Switch.Tick += new System.EventHandler(this.timer_Switch_Tick);

    private void timer_Switch_Tick(object sender, EventArgs e)
{
    motorSwitchControl.Init();        // globally available Switch status                                               
    SwitchRight = SwitchOK("right");
    SwitchRightOK.Visible = SwitchRight; 

    SwitchLeft = SwitchOK("left");    // globally available Switch status
    SwitchLeftOK.Visible = SwitchLeft;  
    SwitchAllOK = SwitchRight & SwitchLeft;
    this.Refresh();
}

a) Is it correct, that the Sleep() actually happens in the Worker Thread? - no Main Thread

b) What is going wrong, if I manipulate user interface objects in DoWork? (Contrary to MSDN Note) - works in Main Thread?

c) What is the correct way to periodically update a PictureBox? DoWork, ProgressChanged, RunWorkerCompleted...? - Three possibilities from laszlokiss88 answer.

Upvotes: 2

Views: 5152

Answers (2)

MoonKnight
MoonKnight

Reputation: 23831

For a start you should almost never have a need to put an active background thead to sleep. I am also not sure why you are constructing/defining the delegate this way, try some thing like

public delegate void UpdatePictureBox();
myDelegate = new UpdatePictureBox(UpdatePictureboxMethod);

then you have a method UpdatePictureBoxMethod

private void UpdatePictureBoxMethod()
{
    this.pictureBox1.Image = Properties.Resources.SomeImage;
}

or something simalar, where you pass in the image to update to.

Alternatively you could use the (bgWorker as BackgroundWorker).ReportProgress(progress, object); method. So from the background thread you call

(bgWorker as BackgroundWorker).ReportProgress(progressBarValue, infoBall);

where here class IfoBall will hold all your important information

class InfoBall
{
    public int nProgressBar { get; set; } 
    public int nMaxProgressBar { get; set; }
    public Image image { get; set; }
}

then you can pass this object back to the UI thread and do your updates

void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    // On UI thread.
    InfoBall someBall = (InfoBall)e.UserState;
    this.pictureBox1.Image = someBall.image;
    // etc...
}

I hope this helps.

Upvotes: 1

laszlokiss88
laszlokiss88

Reputation: 4081

You can update the UI from the DoWork event via the Dispatcher, or Control.Begininvoke(winforms), or you can do it via the ProgressChanged event of the BackgroundWorker:

    public MainWindow()
    {
        InitializeComponent();

        var bw = new BackgroundWorker();
        bw.WorkerReportsProgress = true;
        bw.DoWork += new DoWorkEventHandler(bw_DoWork);
        bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
        bw.RunWorkerAsync();
    }

    void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        // You are in the main thread
        // Update the UI here
        string data = (string)e.UserState;
    }

    void bw_DoWork(object sender, DoWorkEventArgs e)
    {
        // You are in a worker thread
        (sender as BackgroundWorker).ReportProgress(0, "right");
    }

Upvotes: 5

Related Questions