0

When creating new Thread ,how to pass a method which have TextBoxes as parameters??

private void waits(TextBox t1 ,TextBox t2)
{
    t1.BackColor = Color.Red;
    t2.BackColor = Color.Red;
}

Thread n1=new Thread(waits)
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
  • 2
    Are you sure that you really want to do this. Most .NET UI frameworks do not allow you to modify a control, like a textbox from a non-UI thread. – Ade Miller Sep 15 '13 at 13:52
  • Are you asking how to pass two parameters to a thread proc? See http://stackoverflow.com/a/18815486/56778 – Jim Mischel Sep 15 '13 at 22:50

1 Answers1

0

Use Invoke

private void waits(TextBox t1 ,TextBox t2)
{
    t1.Invoke(new Action(()=>
    {
        t1.BackColor = Color.Red;
    });
    t2.Invoke(new Action(()=>
        t2.BackColor =  Color.Red;
    });
}
Alessandro D'Andria
  • 8,663
  • 2
  • 36
  • 32
  • Does this makes any sense? Passing control to worker thread and back again to UI thread. I know if we do it in worker thread you'll get exception. but this doesn't makes any sense – Sriram Sakthivel Sep 15 '13 at 14:01
  • @SriramSakthivel, Does this make any sense? I don't know, but if the OP want to update a control from another Thread this is (one) of the way, know if you ask 'why do that?', no idea. – Alessandro D'Andria Sep 15 '13 at 14:06
  • 1
    Your answer doesn't answers the question. pls read the question again. BTW two invoke calls are not required here, once you update the answer I'll remove my downvote – Sriram Sakthivel Sep 15 '13 at 14:08
  • @SriramSakthivel, you are absolutely right, i didn't understand what OP asks, thanks. – Alessandro D'Andria Sep 15 '13 at 14:11
  • Invoke() is dangerous, it is apt to cause deadlock. And expensive. You only need it when you need its return value, certainly not the case here. Use BeginInvoke() instead. – Hans Passant Sep 15 '13 at 17:31