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 8421759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:14:13+00:00 2026-06-10T03:14:13+00:00

Background From the valuable advice I received here I have now moved all of

  • 0

Background

From the valuable advice I received here I have now moved all of my database intensive code to a backgroundworker, specifically the direct calls to the database. That code is executed during the backgroundworker’s DoWork event. If a DataTable is returned during the DoWork event, I set that DataTable to a class-wide variable. This is done, to avoid having to invoke the controls requiring the DataTable every time I run this code.

While that code is being executed, I have a label that is updated in the main UI thread, to let the user know that something is occurring. To update the label I use a timer, such that every 750 ms a “.” is appended to the label’s string.

The first thing that I noticed was that the backgroundworker’s RunWorkerCompleted event wasn’t triggering. To solve this I did an Application.DoEvents(); before each call I made to the backgroundworker. It was ugly, but it caused the event to trigger. If anyone has an alternative to fix this, I am all ears.

I then came across an interesting predicament. If I run the program within Visual Studio 2010, in the debugging mode, I get an InvalidOperationException error stating that the “Cross-thread operation not valid: Control ‘lblStatus’ accessed from a thread other than the thread it was created on.” This error occurs during the backgroundworker’s RunWorkerCompleted event, where I set the text of a label in the main UI thread. But, when I launch the application directly, through the executable, it works exactly as desired (i.e. the label’s text is set correctly).

Question

Can anyone explain what is going on / offer advice on how to improve upon this?

Code

I can’t post all of the code involved, but here’s some relevant stuff:

namespace Test
{
    public partial class frmMain : Form
    {
        public static Boolean bStatus = false;
        static Boolean bTimer = false;
        System.Timers.Timer MyTimer = new System.Timers.Timer();

        public frmMain()
        {
            InitializeComponent();
            MyTimer.Elapsed += new System.Timers.ElapsedEventHandler(MyTimer_Elapsed);
            MyTimer.Interval = 750; // Every 3/4 of a second
            ExampleTrigger();
        }

        /// <Insert>Lots of unshown code here</Insert>

        private void ExampleTrigger()
        {
            // This is used to simulate an event that would require the backgroundworker
            Application.DoEvents();
            bgw.RunWorkerAsync(0);
            WaitText("Example - 1");
        }

        private static void MyTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            bTimer = true;
        }

        // Update status text
        private void WaitText(string txt)
        {
            MyTimer.Enabled = true;
            lblStatus.Text = txt;
            bStatus = false;
            while (!bStatus)
            {
                if (bTimer)
                {
                    txt = txt + ".";
                    lblStatus.Text = txt;
                    lblStatus.Update();
                    bTimer = false;
                }
            }
            MyTimer.Enabled = false;
        }

        private void bgw_DoWork(object sender, DoWorkEventArgs e)
        {
            int iSelect = (int)e.Argument;
            switch (iSelect)
            {
                case 0:
                    // Hit the database
                    break;
                /// <Insert>Other cases here</Insert>
                default:
                    // Do something magical!
                    break;
            }
        }
        private void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            bStatus = true;
            lblStatus.Text = "Ready!";  // This is where the exception occurs!
        }
    }
}
  • 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-10T03:14:15+00:00Added an answer on June 10, 2026 at 3:14 am

    Never run a while() loop like that in the UI thread.
    You’re freezing the UI until the loop terminates; this defeats the purpose.

    In addition, System.Timers.Timer doesn’t run callbacks in the UI thread.
    Use a WinForms Timer instead.

    Once you switch to a WinForms timer, you can simply append to the label inside the timer callback, and disable the timer when the operation finishes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background I came from several years working in a company where all the database
I come from Java Background and so used to Debugging using Eclipse but have
Here is what I want to make, somehow: 1- A gradient background from top
I have a background from languages which use {} to say that these are
is it possible to change the iPhone background from code?
I have a UITableviewCell that loads its background from an image, my question is,
I've made a button background from shapes and is looking quite good for my
My programming background comes from C and some C++ programming with strong emphasis on
I want to load 4 images from background showing a load bar to the
I'm trying to make simple website with content background combined from 3 images: top

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.