Cross Threaded form access

need to update form from seperate thread

Page 1 of 1

3 Replies - 1165 Views - Last Post: 24 July 2010 - 03:48 PM Rate Topic: -----

#1 SpiderSpartan  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 120
  • Joined: 06-February 08

Cross Threaded form access

Posted 24 July 2010 - 09:34 AM

I have a form that displays information about the parallel port. I created a separate thread that will run an infinite loop polling the parallel port and update the form accordingly. The problem is I keep getting the following error:

"An unhandled exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll

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

Here is the code I'm currently running:

private void Form1_Load(object sender, EventArgs e)
{
     ThreadStart Watch = new ThreadStart(Monitor);
     Thread thread = new Thread(Watch);
     thread.Start();
}

public void Monitor()
{
     int INF = 0;
     while (INF <= 10)
     {
          txtIn.Text = INF.ToString();
          Application.DoEvents();
          Thread.Sleep(6000);
          INF += 1;
     }
}



This is all in the form class and the while loop will eventually be infinite and include polling the parallel port. So how can I gain access to the form objects from this separate thread? Thanks for any help.

Is This A Good Question/Topic? 0
  • +

Replies To: Cross Threaded form access

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Cross Threaded form access

Posted 24 July 2010 - 09:52 AM

I posted something on my blog about this very topic, take a look.

Second option is to use a delegate, like this

private delegate void UpdateLabelTextDelegate(Label ctrl, string value);

private void UpdateLabelText(Label ctrl, string value)
{
    if (ctrl.InvokeRequired)
        ctrl.Invoke(new UpdateLabelTextDelegate(UpdateLabelTextCallback), new object[] { ctrl, value });
    else
        UpdateLabelTextCallback(ctrl, value);
}

private void UpdateLabelTextCallback(Label ctrl, string value)
{
    ctrl.Text = value;
}



Then in your Monitor method

public void Monitor()
{
    int INF = 0;
    while (INF <= 10)
    {
        UpdateLabelText(txtIn.Text, INF.ToString());
        Application.DoEvents();
        Thread.Sleep(6000);
        INF += 1;
    }
}




Hope that helps :)
Was This Post Helpful? 1
  • +
  • -

#3 SpiderSpartan  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 120
  • Joined: 06-February 08

Re: Cross Threaded form access

Posted 24 July 2010 - 03:45 PM

Thank you so much for the response. I couldn't get the Extension Method to work. It gave me a lot of errors, but I did get the delegate method working. It's been a while since I've worked in C#. Now I should have everything I need to finish this project.
Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Cross Threaded form access

Posted 24 July 2010 - 03:48 PM

What errors did you get, because I've used that Extensions method so many times with no fail.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1