多线程:“对象当前正在其他地方使用”怎么样解决

.Net技术 码拜 8年前 (2016-02-26) 4382次浏览
–程序开始运行–
2016-06-17 13:34:51,109 [18] FATAL [(null)] – 捕获到未处理的异常!公共语言运行库即将终止!
System.InvalidOperationException: 对象当前正在其他地方使用。
在 System.Drawing.Graphics.CheckErrorStatus(Int32 status)
在 System.Drawing.Graphics.FillRectangle(Brush brush, Int32 x, Int32 y, Int32 width, Int32 height)
在 System.Drawing.Graphics.FillRectangle(Brush brush, Rectangle rect)
在 Sunisoft.IrisSkin.xead0c12c344036e3.x8bc95f030953f87b(Graphics x4b101060f4767186)
在 Sunisoft.IrisSkin.xead0c12c344036e3.PaintControl()
在 Sunisoft.IrisSkin.xbd3f2493841f18a1.x02b398f1260ef2d9(Object xe0292b9ed559da7d, EventArgs xfbf34718e704c6bc)
在 System.Windows.Forms.Control.OnEnabledChanged(EventArgs e)
在 System.Windows.Forms.ButtonBase.OnEnabledChanged(EventArgs e)
在 System.Windows.Forms.Control.set_Enabled(Boolean value)
在 WeiPuSMS.View.SMSTool.frmMain.ContectDevices() 位置 F:\Project\trunk\Tool\frmMain.cs:行号 3408
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()
改人家一个项目, Framework 4.0, vs2010。 在 Win 10/Win7 64bit 下都没有问题,但在 Win XP 32bit 下就报这个错。
问一下大家有遇到过的么?
解决方案

1

首先,btnConn这看上去是按钮吧?那就应该让主线程去访问,而不是其他的子线程。
全部子线程都不允许访问主线程所创建的控件的。

5

封装一个方法,放到持有btnCon的线程去执行。WPF不允许多线程直接同时访问一个对象,有委托封装一下。

34

试试:

this.Invoke(new Action(() =>
{
	btnConn.Enabled = true;
}));

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明多线程:“对象当前正在其他地方使用”怎么样解决
喜欢 (1)
[1034331897@qq.com]
分享 (0)