开发者

Same project, different machines, one machine has cross threading issue

开发者 https://www.devze.com 2023-01-06 11:43 出处:网络
I have two machines, one is an old toshiba laptop that I use at home for development, the other is a 1 year old dell at work.Both are running the same version of visual studio 2008.I have been, for th

I have two machines, one is an old toshiba laptop that I use at home for development, the other is a 1 year old dell at work. Both are running the same version of visual studio 2008. I have been, for the last several months, building a project for a client. It runs fine on my old laptop, but if I run it in the same environment at work I get cross threading issues, anybody have an idea why? Is there something in the project/environment that I need to check or uncheck...

The problem has occurred ever since I added a tab control to the main user interface. The cross threading error occurs when loadin开发者_JAVA百科g the main form, when the tab control is dynamically resized - I have made it react like an expandable panel so the user can hide it if it is not being used.

This is the line is fails on, this is in the main load section:

expandable_tabcontrol.Left += expandable_tabcontrol.Width;

This is the error message:

An unhandled exception of type 'System.InvalidOperationException' 
occurred in System.Windows.Forms.dll

Additional information: Cross-thread operation 
not valid: Control 'frmUserInterFace' accessed 
from a thread other than the thread it was created on.

Runs fine at home, fails at work...

Thanks, R.


It's difficult to be certain without a little more information, but it sounds to me like the method that does the resizing can be called in more than one place. It's likely that your old Toshiba is a single-core machine, so the computer can't do more than one thing at once and the tab is resized by the main thread. On your newer Dell machine, which likely has multiple cores, a secondary thread is calling the method that does the resizing, and that's causing the exception to be thrown.

Look into the use of Control.InvokeRequired and Control.Invoke to handle synchronization between background threads and the UI thread.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号