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.

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 });
    else
      this.label1.Text = msg;
}

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.