Click here to Skip to main content
15,920,708 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted

You can only use any control from the UI thread: the thread it was created on. If you aren't on the right thread, then you need to invoke the correct one.

How to: Make Thread-Safe Calls to Windows Forms Controls [^]

[edit]Typo: "controlled" for "created" - OriginalGriff[/edit]
 
Share this answer
 
v2
Comments
serigraphie 25-May-11 9:10am    
Hi OriginalGriff,
Thank you for your response and I will study the link you have given me.
Thank you.
Sergey Alexandrovich Kryukov 25-May-11 11:51am    
My 5.

Please see my answer (links) a wider view, more detail on the mechanism.
--SA
In the function where you are updating label1, do this:

C#
this.Invoke(new MethodInvoker(UpdateLabel)); //UpdateLabel is a function where the label will be updated

private void UpdateLabel()
{
 //Do something with the label1
}
 
Share this answer
 
Comments
serigraphie 25-May-11 9:09am    
Hi Tarun,
Thank you very much for your response and simply answering my question, still I dont get it, I am not the one who did the codes for this, I hope this may help you to help me, here is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

using System.Reflection;
namespace BCCSReportGenerator
{
public partial class frmComputeBCCS : Form
{
object[] _rowsFilledArray = new object[] { 2 };
public delegate void SetRowsFilledDelegate(string val);
public SetRowsFilledDelegate SetRowsFilled;
Thread _oFillThread = null;




public frmComputeBCCS()
{
InitializeComponent();
SetRowsFilled = new SetRowsFilledDelegate(OnSetRowsFilled);
}
public void OnSetRowsFilled(string val)
{
label1.Text = val;
//progressBar1.Value = val;

}
private void frmComputeBCCS_Load(object sender, EventArgs e)
{
//GenerateReport();
label1.Text = "";
label2.Visible = false;
}
private void BeginThread()
{
progressBar1.Maximum = 200;
progressBar1.Minimum = 0;
timer1.Enabled = true;
_oFillThread = new Thread(new ThreadStart(GenerateReport));
//btnExecute.Enabled = false;
_oFillThread.Start();

}
void GenerateReport()
{
try
{
string UserName = StaticObject.UserName;


DataAccess AccessData = new DataAccess();
DataSet _ds = AccessData.PopulateReports(UserName);
if (_ds.Tables[0].Rows.Count > 0)
{
DataTable DT = _ds.Tables[0];
label1.Text = "";
int iCount = 0;
foreach (DataRow row in DT.Rows)
{
DataSet _ds1 = new DataSet();
string ReportName = row[0].ToString();
string DateGenerated = DateTime.Now.ToShortDateString();
_rowsFilledArray[0] = "Computing " + ReportName;
//_rowsFilledArray[1] = DT.Rows.Count.ToString();
this.BeginInvoke(SetRowsFilled, _rowsFilledArray);
//_rowsFilledArray2[0] = iCount;
//this.BeginInvoke(SetRowsFilled2, _rowsFilledArray2);
//BG
if (row[2].ToString() == "Business Group")
{
AccessData.GenerateReport(ReportName, DateGenerated, row[2].ToString(), row[1].ToString(), UserName);
}
else if (row[2].ToString() == "Team")
{
//TEAM
AccessData.GenerateReport(ReportName, DateGenerated, row[2].ToString(), row[1].ToString(), UserName);
}
else if (row[2].ToString() == "Division Managed")
{

//Division Managed
AccessData.GenerateReport(ReportName, DateGenerated, row[2].ToString(), row[1].ToString(), UserName);
}
else if (row[2].ToString() == "Family")
{
//Family
AccessData.GenerateReport(ReportName, DateGenerated, row[2].ToString(), row[1].ToString(), UserName);
}
iCount = iCount + 1;
if (iCount == DT.Rows.Count)
{
timer1.Stop();
}
}
MessageBox.Show("You have
Sergey Alexandrovich Kryukov 25-May-11 11:50am    
Right, a 5, please see more detail in my answer (links).
--SA
You cannot call anything in UI from a non-UI thread. You can use BeginInvoke or Invoke of System.Threading.Dispatcher (both forms and WPF, preferred) or System.Windows.Forms.Control (Forms only, apparently).

See detailed explanation and samples code in my past asnwers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

More links on thread use:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA
 
Share this answer
 
Comments
Tarun.K.S 25-May-11 13:08pm    
Aah I remember this answer, good one and detailed. 5+
Sergey Alexandrovich Kryukov 25-May-11 13:50pm    
Well, this on is modified specially for OP.
Thank you, Tarun.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900