Set Value of Textbox

enginious

Newcomer
Joined
Jan 9, 2007
Messages
11
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 :

Code:
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.
 
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.
 
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 :

Code:
        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 :

Code:
        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.
 
It's being called from the method :

private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)

Thanks,
Sparky.
 
You will need to check if the Control requires invoking like so:

Code:
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);
 
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.
 
Back
Top