Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8587269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:31:45+00:00 2026-06-11T22:31:45+00:00

I’m using a BackgroundWorker to periodically check a hardware switch. Due to it is

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T22:31:46+00:00Added an answer on June 11, 2026 at 10:31 pm

    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");
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have thousands of HTML files to process using Groovy/Java and I need to
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I am reading a book about Javascript and jQuery and using one of the
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.