Share via


how to call a function from a existing thread plz help

Question

Tuesday, September 11, 2012 1:44 PM

hi i am calling a function but it is giving me error:
Cross-thread operation not valid: Control 'textBox1' accessed from a thread other than the thread it was created on.
plz help

private void button1_Click(object sender, System.EventArgs e)
        {
            _t = new Thread(new ThreadStart(DoSomething));
            _t.Start();
        }
private Thread _t;
        private void DoSomething()
        {
            try
            {
                while(true)
                {
                   write(str);
                }
            }
            catch (ThreadAbortException ex)
            {
                MessageBox.Show("Loop break");
                return;
            }
        }
 public void WriteText(string str)
{

texbox=str;

/*do something*/
}

 private void button2_Click(object sender, System.EventArgs e)
  {
            _t.Abort();
  }

m

All replies (3)

Tuesday, September 11, 2012 2:04 PM ✅Answered | 2 votes

As JohnWein said, use Invoke through a delegate.

Please find the code sample.

      private Thread Strt;        public delegate void UpdateProcessDelegate(TextBox t, string str);        private void button1_Click(object sender, EventArgs e)        {            //Use a delegate to call the thread             UpdateProcessDelegate updatedel = new UpdateProcessDelegate(UpdateProcess);            ThreadStart method = new ThreadStart(() => updatedel(textBox1, "text box display value"));            Strt = new Thread(method);            Strt.Start();        }        public void UpdateProcess(TextBox t, string str)        {                  if (t.InvokeRequired)            {                UpdateProcessDelegate method = new UpdateProcessDelegate(UpdateProcess);                t.Invoke(method, new object[] { t,str });                return;            }            t.Text = str;        }

Please mark this post as answer if it solved your problem. Happy Programming.


Tuesday, September 11, 2012 2:08 PM ✅Answered | 2 votes

You cannot access a UI element on any thread other than the one that created it.  The error you got indicates you're doing just that.  Your DoSomething method (which is running on a non-UI thread) cannot touch anything in the UI.  To communicate back to the UI you have to marshal the request.  Depending upon what you're doing there are numerous ways to solve this issue.

BackgroundWorker - great for running 1 off tasks that require cancellation and/or progress notification

Task - great for chaining requests together and having some run on the UI thread

Invoke - useful generally only for a single marshal back to the UI thread

MSDN has lots of examples on how to use all these and how to perform work on a background thread and update a UI thread in the process.  Please refer to MSDN for examples based upon your specific needs.

Michael Taylor - 9/11/2012
http://msmvps.com/blogs/p3net


Tuesday, September 11, 2012 1:56 PM | 2 votes

Invoke WriteText throgh a delegate.