Hello everyone, I have here a standalone application written using C# language.
Error occured when I hit the button "compute"

Please see below error:

Error Description : System.InvalidOperationExcepton: Cross-thread operation not valid: Control 'label1' accessed froma thread other than the thread it was created on.
at system.windows.forms.control.get_Handle()
at System.Windows.Forms.Control.set_WindowText(String value)
at System.Windows.Forms.Control.set_text(String Value)
at BCCSReportGenerator.frmComputeBCCS.GenerateReport() in D:\PROCUREMENT SYSTEMS BACKUP\TOOLS AND INSTALLERS\PROJECTS\BCCS Tool\Source Code\BCCSReportGenerator\frmComputeBCCS.cs line 62

Thank you very much in advance for your help.

7 Years
Discussion Span
Last Post by Mitja Bonca

You will have to use delegates to update the Text property of the label1. Because you want to access from another thread to the main one and update the label`s Text property, you get this error.
You can do it like this:

//delegate void MyDelegate(string message);

private void MyMethod(string msg)
     if (this.label1.InvokeRequired)
         this.label1.Invoke(new MyDelegate(MyMethod), new object[] { msg });
      this.label1.Text = msg;

Call this method from where do you want to update label.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.