Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi,

 

I am working with c#.net after using VB6, and apart from a couple of problems it's not been bad, but I just came across this problem. I am trying to set the value of the textbox :

 

TextCounter.Text = Properties.Settings.Default.SoftwareCounterValue.ToString();

 

Error:"Cross-thread operation not valid: Control 'TextCounter' accessed from a thread other than the thread it was created on."

 

Yet, I can set the value of the box to 0 when I click a button, which I was assuming was also on a different thread - is this not the case?

 

If you have any ideas then that would be great - I seem to have more questions than answers at the moment.

 

Cheers,

Sparky.

  • Administrators
Posted

Where is the code you posted being called from? This error is generated if you try to update the UI from anything other than the main UI thread. Are you using any async callbacks or threads in your code?

 

If so you will need to check if InvokeRequired is true and if so get the main UI thread to call your code - search these forums for InvokeRequired and you should get a few useful hits.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

Thanks

 

Hi, thanks for the reply, I dont think I'm doing Async callbacks, not that I know of, but if you have time I've posted the code here :

 

       public void ProcessOutPut(string MyString)
       {
            //Update counter
           if (SoftwareCounter.Checked == true)
           {
               Properties.Settings.Default.SoftwareCounterValue++;
               Properties.Settings.Default.Save();
               TextCounter.Text = Properties.Settings.Default.SoftwareCounterValue.ToString();
           }
       }

 

It's the last line that is when the error occurs. But the thing I dont understand is that I can do this, which apparently causes no error, yet as far as I can see it's still setting the value of the text box in essentially the same way :

 

       public void BtnCounterReset_Click(object sender, EventArgs e)
       {
           Properties.Settings.Default.SoftwareCounterValue = 0;
           Properties.Settings.Default.Save();
           TextCounter.Text = Properties.Settings.Default.SoftwareCounterValue.ToString();
       }

 

I appreciate your help, as no matter how much I try and find the answer, so far I've had no luck.

Cheers,

Sparky.

Posted

You will need to check if the Control requires invoking like so:

 

System.Windows.Forms.Control control = new System.Windows.Forms.Control();
           System.Windows.Forms.MethodInvoker updateControl = delegate
           {
               //update code here...
           };
           if(control.InvokeRequired)
               control.Invoke(updateControl);

Posted

Resolved and thanks

 

Hi Kurf,

 

Thank you for your reply, that worked a treat - and thank you to PlausiblyDamp as well for your assistance.

 

I know it was such a small thing not to be able to do, but no matter how many tutorials and guides that I tried to understand, I just couldnt figure it out.

 

That's another thing off my list, and I can now try and progress further, so thanks guys.

 

BTW, do I need to mark this thread as resolved now or something or is that not necessary?

 

Cheers,

Sparky.

  • *Experts*
Posted

We don't usually mark threads as resolved or "still need assistance". Might be useful, but we've just never done it.

 

-ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...