By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,978 Members | 1,467 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,978 IT Pros & Developers. It's quick & easy.

Blank tab page when switching tab pages inside a thread

P: n/a
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
Nov 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"Johann Blake" <jo*********@yahoo.com> wrote in message
news:d8************************@posting.google.com ...
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/...#windows.forms for
more information.

Regards,
Sami
Nov 16 '05 #2

P: n/a
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" wrote:

"Johann Blake" <jo*********@yahoo.com> wrote in message
news:d8************************@posting.google.com ...
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/...#windows.forms for
more information.

Regards,
Sami

Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.