update a control in UI with running background Thread in Winforms

13,825

Solution 1

In WinForms (WPF as well) UI controls can only be updated in the UI thread. You should update your label this way:

public void UpdateLabel(String text){
    if (label.InvokeRequired)
    {
        label.Invoke(new Action<string>(UpdateLabel), text);
        return;
    }      
    label.Text = text;
}

Solution 2

Inside your Do_Work method, you can use the object's Invoke() method to execute a delegate on its UI thread, e.g.:

this.Invoke(new Action<string>(UpdateLabel), newValue);

...and then make sure to add a method like this to your class:

private void UpdateLabel(string value)
{
    this.lblMyLabel.Text = value;
}
Share:
13,825
yogendra
Author by

yogendra

Over 12 years of experience in Software development in various Technologies including role of Scrum Master. Expert in developing applications using Microsoft Visual Studio as IDE and C#.NET, VB.Net, ASP.NET and Win forms as technologies and SQL Server database. Good experience in Web based development using Python, Django and Flask. Investment Banking domain experience which included knowledge of Fixed Income, Capital Market , Credit Risk &amp; Custody Services Proficient in Web Services, JavaScript and REST API. Worked on Python for task automation and automated job processing. Experience in managing Scrum Team to facilitate daily Scrum, Sprint Planning, Sprint Demo and Sprint Retro meetings. Knowledge of CI/CD Process with exposure of DevOps tools like UDeploy, TeamCity &amp; SonarCube. Knowledge of Amazon Web Services (AWS), EC2, EBS, RDS, Lambda, S3 etc. Worked on SSIS (SQL Server Integration Services) and Crystal Reports. Certified Scrum Master. Involved in various phases of application development including Requirement gathering, analysis, design and development Strong fundamentals related to Object oriented programming concepts. Active Team player and as an individual with Good Communication and interpersonal skills. Excellent problem resolution and communication skills with ability to work under pressure in a highly visible role. Adaptive to team environment and has the capability of completing complex tasks independently. Self-learning abilities, Good communication and interpersonal skills, highly motivated and productive team worker.

Updated on June 07, 2022

Comments

  • yogendra
    yogendra almost 2 years

    I am using Background Worker Thread in my Winform, inside my Do_Work Event I am calculating something, what I need the thing is that simultaneously I would like to update a label which is in main/UI thread ? how to achieve this?

    I want to update my label from Do_Work event...

  • yogendra
    yogendra almost 12 years
    Sorry to say but this code is really not helpful even this code is giving an exception at line - " backgroundWorker1.ReportProgress(0); "
  • Hamza_L
    Hamza_L almost 12 years
    did you set WorkerReportsProgress to true ? if you did, please give me the exception message