enginious Posted January 12, 2007 Posted January 12, 2007 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. Quote
Administrators PlausiblyDamp Posted January 12, 2007 Administrators Posted January 12, 2007 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
enginious Posted January 12, 2007 Author Posted January 12, 2007 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. Quote
Administrators PlausiblyDamp Posted January 12, 2007 Administrators Posted January 12, 2007 Where is the ProcessOutPut method being called from? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
enginious Posted January 13, 2007 Author Posted January 13, 2007 It's being called from the method : private void port_DataReceived(object sender, SerialDataReceivedEventArgs e) Thanks, Sparky. Quote
kurf Posted January 13, 2007 Posted January 13, 2007 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); Quote
enginious Posted January 15, 2007 Author Posted January 15, 2007 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. Quote
*Experts* Nerseus Posted January 15, 2007 *Experts* Posted January 15, 2007 We don't usually mark threads as resolved or "still need assistance". Might be useful, but we've just never done it. -ner Quote "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
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.