G
Guest
I'm having some difficulty getting controls to work with multithreading.
Basically, I want to begin a long running operation on another thread when a
button is clicked, and then disable that button until the operation
completes. When the operation finishes, I want the button to re-enable. To
make things more interesting, this button is located on a TabPage in a
TabControl on the main Form, so it's three containers deep. I'm using C# 2.0
Beta 2. Here's basically what my code looks like:
// Button clicked event handler
public void btnRun_click(/*params*/)
{
btnRun.enabled = false;
Thread query = new Thread(new ThreadStart(operation));
query.start();
}
private void operation()
{
/* Do a long running SQL query */
this.Parent.Parent.Parent.BeginInvoke(done());
}
private Delegate done()
{
btnRun.enabled = true;
}
When I run this code, btnRun.enabled = true throws an exception saying that
I can't access the Button from a different thread than it was created on.
However, I was under the impression that that whole point of
Control.BeginInvoke was to run a method on the same thread that the control
was created on. I've tried this using the BeginInvoke method of the Form,
the TabControl, and the TabPage, and get the same result on all three.
Anyone know how to resolve this? Thanks!
Basically, I want to begin a long running operation on another thread when a
button is clicked, and then disable that button until the operation
completes. When the operation finishes, I want the button to re-enable. To
make things more interesting, this button is located on a TabPage in a
TabControl on the main Form, so it's three containers deep. I'm using C# 2.0
Beta 2. Here's basically what my code looks like:
// Button clicked event handler
public void btnRun_click(/*params*/)
{
btnRun.enabled = false;
Thread query = new Thread(new ThreadStart(operation));
query.start();
}
private void operation()
{
/* Do a long running SQL query */
this.Parent.Parent.Parent.BeginInvoke(done());
}
private Delegate done()
{
btnRun.enabled = true;
}
When I run this code, btnRun.enabled = true throws an exception saying that
I can't access the Button from a different thread than it was created on.
However, I was under the impression that that whole point of
Control.BeginInvoke was to run a method on the same thread that the control
was created on. I've tried this using the BeginInvoke method of the Form,
the TabControl, and the TabPage, and get the same result on all three.
Anyone know how to resolve this? Thanks!