Problem changing tabs in tab controll from thread

S

Steve Bonner

I have a simple windows form app written in c# that contains a tab
controll with two tabs. When the app starts, it spawns a new thread
which checks a file every couple of seconds, if the file changes, the
thread changes the selected tab in the main form.

In the form, the tab changes, but does not drawn its contents
correctly (graphics from the previous tab show through). The tab
control is also no longer responsive. And if I click on it, the
application becomes unresponsive.

If I change the tab from the application directly, everything works
fine.

Does anyone have any suggestions on how I can debug this? or what
might be going on?

mike c
 
T

Tim Wilson

You should not update controls from a thread other than the one that "owns"
the control handle. In changing anything related to the GUI the update
should take place on the GUI thread. This means that you should be
"invoking" a delegate so that the method to handle GUI changes is executed
on the GUI (main) thread. So try passing a delegate into the new thread and
call this delegate when the file has changed. From the method that has been
associated with this delegate you can call tabControl1.Invoke to actually
have the code to update the tab execute on the proper thread.
 
N

Nico Vrouwe

Hi,

It might have something to do with that you access the contents from a
different thread than the one that created the tab control.
You could try monitoring changes to the file using a
FileSystemWatcher.Changed event and keep the code in the same thread.

Hope this helps,
/Nico
 
S

Steve Bonner

Thanks. I wasn't aware of this API, and it sounds like it is exactly
what I need (and I won't need the additional thread anymore).

mike c
 
S

Steve Bonner

Thanks. This sounds like the problem. I appreciate the info on
passing a delegate (now I won't run into similar issues in the
future).

mike c
 
S

sb

Doh! This was actually related to my original question. I put two and
two together, used a delegate and everything is working perfectly now.

thanks for everyone's help..

mike c
 

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