System.InvalidOperationException: 'Cross-thread operation not valid: Control 'EstimatedBatteryResultLabel' accessed from a thread other than the threa

  • Thread starter Thread starter Satyaprakash A
  • Start date Start date
S

Satyaprakash A

Guest
Hi,

When i am running the application using the below C# code, i am getting an exception

System.InvalidOperationException: 'Cross-thread operation not valid: Control 'EstimatedBatteryResultLabel' accessed from a thread other than the thread
it was created on.'

at the line

this.EstimatedBatteryResultLabel.Text = UpdateTimeOnEstimatedBatteryPanel();


if ((UIControlObj.MGDIsRuntimeToEmpty()) || (UIControlObj.MGDIsACPresent()))
{
this.EstimatedBatteryResultLabel.Text = UpdateTimeOnEstimatedBatteryPanel();
}

Calling the Below function and returning value from this function and then getting that exception.


private string UpdateTimeOnEstimatedBatteryPanel()
{
if (!(UIControlObj.MGDIsWorkerProcessRunning()))
{
return m_ResourceManager.GetString("IDS_PANEL_0_MINUTES");
}
string remainingBatteryTime = string.Empty;
double sec = UIControlObj.MGDGetRemainingBatteryTime();
int bTime = Convert.ToInt32(sec) / UIHelper.APC_SIXTY_SECONDS;
if (sec > UIHelper.APC_SIXTY_SECONDS)
{
string remainingBatteryTimeString = m_ResourceManager.GetString("IDS_PANEL_MINUTES");
remainingBatteryTime = string.Format(remainingBatteryTimeString, bTime);
}
else
{
remainingBatteryTime = m_ResourceManager.GetString("IDS_PANEL_LT_1_MINUTE");
}
return remainingBatteryTime;
}


We are setting the Label "EstimatedBatteryResultLabel" in InitializeComponent() function like as shown below:

private System.Windows.Forms.Label EstimatedBatteryResultLabel;

private void InitializeComponent()
{
this.EstimatedBatteryResultLabel = new System.Windows.Forms.Label ();

this.EstimatedBatteryResultLabel.Font = new System.Drawing.Font ("Lucida Sans Unicode", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.EstimatedBatteryResultLabel.ForeColor = System.Drawing.Color.FromArgb (((int)(((byte)(11)))), ((int)(((byte)(14)))), ((int)(((byte)(79)))));
this.EstimatedBatteryResultLabel.Location = new System.Drawing.Point (45, 41);
this.EstimatedBatteryResultLabel.Name = "EstimatedBatteryResultLabel";
this.EstimatedBatteryResultLabel.Size = new System.Drawing.Size (142, 30);
this.EstimatedBatteryResultLabel.TabIndex = 0;
this.EstimatedBatteryResultLabel.Text = "25 min";
this.EstimatedBatteryResultLabel.TextAlign = System.Drawing.ContentAlignment.TopCenter;\
}

Please help me to find the root cause of this error.

Continue reading...
 
Back
Top