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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:04:09+00:00 2026-06-03T00:04:09+00:00

I am working on a Winform Application. The Method is started by a BackgroundWorker

  • 0

I am working on a Winform Application. The Method is started by a BackgroundWorker Thread. I am sorry. I did not mention this earlier.

private void Method()
{
 tasks[i] = Task.Factory
           .StartNew(() => fileProcessor.ProcessEachMachine(mdetail))
           .ContinueWith(UpdateLabel, TaskContinuationOptions.OnlyOnRanToCompletion);
}

I have a long running function ProcessEachMachine. In the continuation function UpdateLabel I want to access UIlabel and update the status.

private void UpdateLabel()
{
   progressLbl.Text = "updated";
}

But the label is not getting updated. How to access UILabel and update the text of it.?

  • 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-03T00:04:11+00:00Added an answer on June 3, 2026 at 12:04 am

    You have to set the TaskScheduler.FromCurrentSynchronizationContext on ContinueWith or else it will not be run in the UI context. Here is the MSDN on the override that you must use for this call to ContinueWith.

    It should end up looking like this:

    .ContinueWith(UpdateLabel, null, 
        TaskContinuationOptions.OnlyOnRanToCompletion,
        TaskScheduler.FromCurrentSynchronizationContext());
    

    It may seem like nothing is happening, but the TPL is currently swallowing your cross thread exception. You should probably use the UnobservedTaskException if you are not going to inspect each result or check for its exception. Otherwise, when garbage collection occurs, the exception will happen then…which could create hard to debug errors.

    UPDATE

    Based on your update about the main Task being setup and started by a Backgroundworker, my main question is why this could not use a Task to start? In fact, if there is not more in the Method, then this is really just double work and might confuse other developers. You are already started asynchronously, so why not just do your work within the backgroundworker and use an OnComplete method that will UpdateLabel (as background workers are already context aware).

    The main problem is still the same though, so here are some other solutions if you feel you must use the TPL:

    1. You can Invoke back onto the main UI thread within the UpdateLabel method
    2. You can pass the current context into the backgroundworker and use that instead
    3. You can Wait for your original Task to return and then use the worker’s oncomplete event to update the label.

    Here is how I would do this (all pseudo code)

    Background Worker Method:

    Method() called because of Background worker
    
    private void Method()
    {
        fileProcessor.ProcessEachMachine(mdetail);
    }
    
    Wire up background worker's OnRunWorkerCompleted:
    
    if(!e.Cancelled && !e.Error)
        UpdateLabel();
    

    Task only method

    Call Method() from the main thread and just let the TPL do its work :)
    
    Task.Factory.StartNew(() => fileProcessor.ProcessEachMachine(mdetail))
           .ContinueWith((precedingTask)=>{if(!precedingTask.Error)UpdateLabel;}, 
               null, TaskContinuationOptions.OnlyOnRanToCompletion,
               TaskScheduler.FromCurrentSynchronizationContext());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some serious issues with a WinForm application that I'm working on. Currently,
I've recently started working with multi-threaded Winform applications and have run up against a
I am working on a C# WinForm application. I want to trigger some processing
I am working on an application where two threads execute a method from a
I'm working on a WinForm application. I've implemented data-access logic into a library project
I am working on a WinForm Application. The Form has many fields/components but is
This question is related to Winform application deployment and configuration. I am new to
I am working with a WinForm application that was designed by the previous, now
I'm working on a WinForm .Net application with the basic UI that includes toolbar
i am working with a winform application , and in the richbox_textchange i would

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.