Blank tab page when switching tab pages inside a thread

J

Johann Blake

The following is a bug I have discovered using tab pages and threads
and I am looking for a workaround.

Create a new Windows Forms application and add a tab control with two
tab pages. Add a label to each tab page. On the first tab page add a
button. When the button is pressed the code behind the button creates
a thread and starts that thread. The only thing that the thread is to
do is to switch from the first tab page to the 2nd tab page. This is
done by setting the SelectedTab property of the tab control to the 2nd
tab page.

When you run the application, do not click on the 2nd tab page.
Instead, click on the button. The tab page will switch to the 2nd tab
page but it will be blank (or empty). The label that you placed on the
2nd tab page is not visible. It's Visible property is in fact set to
true if you were to interrogate the control's properties.

Now try the whole thing again. Stop the program and start it again.
This time, before clicking the button, use the mouse and select the
2nd tab page. Now go back to the first tab page and press the button.
The label on the 2nd tab page now shows up.

This problem only occurs when the switching is within a thread. If you
do the switching within the button's click event without using a
thread, this problem never occurs. It appears therefore that when the
thread dies, it wipes the label off of the 2nd tab page. All the
methods I've tried to redraw the control fail to redraw it again. In
fact, if you put a message box inside the thread just prior to exiting
the thread, the label will get redrawn but as soon as you click the
message box's OK button and the thread terminates, the label will be
erased from the form. The label still exists, at least in code, but
nothing visual exists.

Any clues how to get around this?

Thanks,
Johann
 
S

Sami Vaaraniemi

Johann Blake said:
The following is a bug I have discovered using tab pages and threads
and I am looking for a workaround.

Create a new Windows Forms application and add a tab control with two
tab pages. Add a label to each tab page. On the first tab page add a
button. When the button is pressed the code behind the button creates
a thread and starts that thread. The only thing that the thread is to
do is to switch from the first tab page to the 2nd tab page. This is
done by setting the SelectedTab property of the tab control to the 2nd
tab page.
[snip]
This problem only occurs when the switching is within a thread. If you
do the switching within the button's click event without using a
thread, this problem never occurs. It appears therefore that when the
thread dies, it wipes the label off of the 2nd tab page. All the
methods I've tried to redraw the control fail to redraw it again. In
fact, if you put a message box inside the thread just prior to exiting
the thread, the label will get redrawn but as soon as you click the
message box's OK button and the thread terminates, the label will be
erased from the form. The label still exists, at least in code, but
nothing visual exists.

Any clues how to get around this?

Yes. Stick to the first golden rule of Windows Forms threads programming:
never access a control or a form directly from a non-UI thread. See Jon
Skeet's FAQ at
http://www.yoda.arachsys.com/csharp/multithreading.html#windows.forms for
more information.

Regards,
Sami
 
G

Guest

I ran into this problem, but I have to use multiple threads due to other
functions of our system. The fix is to make the method that switches the
tabs threadsafe by creating a delegate and using the Forms.Invoke method.

Sami Vaaraniemi said:
Johann Blake said:
The following is a bug I have discovered using tab pages and threads
and I am looking for a workaround.

Create a new Windows Forms application and add a tab control with two
tab pages. Add a label to each tab page. On the first tab page add a
button. When the button is pressed the code behind the button creates
a thread and starts that thread. The only thing that the thread is to
do is to switch from the first tab page to the 2nd tab page. This is
done by setting the SelectedTab property of the tab control to the 2nd
tab page.
[snip]
This problem only occurs when the switching is within a thread. If you
do the switching within the button's click event without using a
thread, this problem never occurs. It appears therefore that when the
thread dies, it wipes the label off of the 2nd tab page. All the
methods I've tried to redraw the control fail to redraw it again. In
fact, if you put a message box inside the thread just prior to exiting
the thread, the label will get redrawn but as soon as you click the
message box's OK button and the thread terminates, the label will be
erased from the form. The label still exists, at least in code, but
nothing visual exists.

Any clues how to get around this?

Yes. Stick to the first golden rule of Windows Forms threads programming:
never access a control or a form directly from a non-UI thread. See Jon
Skeet's FAQ at
http://www.yoda.arachsys.com/csharp/multithreading.html#windows.forms for
more information.

Regards,
Sami
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top