Note that there are some explanatory texts on larger screens.

plurals
  1. POcannot make multiple thread safe calls to windows forms control
    primarykey
    data
    text
    <p>I am enhancing a c# win forms application that interacts with SalesForce CRM via webservices.</p> <p>I have the below code to make a 'thread safe' update to a label on my form:</p> <pre><code> delegate void SetTextCallback(string text); private void SetText(string text) { // InvokeRequired required compares the thread ID of the // calling thread to the thread ID of the creating thread. // If these threads are different, it returns true. if (this.lblAccessStatus.InvokeRequired) { SetTextCallback d = new SetTextCallback(SetText); this.Invoke(d, new object[] { text }); } else { this.lblAccessStatus.Text = text; this.lblAccessStatus.Refresh(); } } </code></pre> <p>My form (called <code>SFDetachifier.cs</code>) has a button execute which takes the dates of two calendar controls then calls:</p> <pre><code>lblAccessStatus.Visible = true; picProgress.Visible = true; string fromDate = dtManualFromDate.Value.ToString("yyyy-MM-dd") + fromTime; string toDate = dtManualToDate.Value.ToString("yyyy-MM-dd") + toTime; string[] arg = new string[] { "S1", fromDate, toDate }; SFCtrl._SessionLog.Append("Manual detach requested on " + SFOrgName + "\n"); SFCtrl._SessionLog.Append("Date range requested: " + fromDate + " to " + toDate + "\n"); bgProcessing_Production.RunWorkerAsync(arg); </code></pre> <p><code>bgProcessing_Production</code> has the below code for the Background worker which includes a call to <code>setText</code></p> <pre><code>private void bgProcessing_Production_DoWork(object sender, DoWorkEventArgs e) { String[] args = (String[])e.Argument; e.Result = args[0]; // clear the datagrid view gvTaskCases.DataSource = null; //gvTaskCases.Rows.Clear(); //if (gvTaskCases.Rows.Count != 0) // { // gvTaskCases.Rows.Clear(); // .Update(); // } SetText("login to SalesForce (on " + SFOrgName + ") ...please wait"); </code></pre> <p>Now, when I run my application I set some dates, then execute the above code by clicking the execute button, it does it's work (calling SalesForce XML web services) and puts the results in a grid.</p> <p>that all works fine, the problem occurrs when I try to run the procedure again (for example with different dates)</p> <p>Then I get an error <strong>'Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on.'</strong></p> <p>I don't understand, I have a delegate setup?</p> <p>What am I doing wrong - I'm guessing that when the backround worker is run again it runs on a new thread, thus being thread unsafe.</p> <p>How can I fix this error please?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload