求助 Cannot access a disposed object.

jsgpt003 2012-11-12 09:02:46
  
public frmTailInBox()//构造函数
{
ShowCoatInfoInDgv = (cci) =>
{
DataRow[] drs = m_dtCoatTail.Select(string.Format(@"CoatId='{0}'", cci.SourceCode));
if (drs.Length == 0)
{
m_dtCoatTail.Rows.Add(new object[] { cci.SourceCode, cci.CoatCode, "", "", "" });
if (dgvCoatTail.Rows.Count > 0)
{
dgvCoatTail.FirstDisplayedScrollingRowIndex = dgvCoatTail.Rows.Count - 1;
dgvCoatTail.ClearSelection();
}
}
};
}

public void Reader2_OnReadEpc_6C_800(sender s ,envent){
......
if (cci.CoatCode != null)
{
this.InvokeExAction<Control, CoatCardInfo>(ShowCoatInfoInDgv, cci);}
}
在上面红色代码处是从一个线程(Reader2_OnReadEpc_6C_800)向UI线程显示数据 但是这里报错

}


public static void InvokeExAction<T, TPrams1>(this T @this, Action<TPrams1> action, TPrams1 p1)
where T : Control
{
if (@this.InvokeRequired)
{
@this.Invoke(action, new object[] { p1 });
}
else
{
if (!@this.IsHandleCreated)
return;
if (@this.IsDisposed)
//throw new ObjectDisposedException("@this is disposed.");
return;
action(p1);
}
}


日志如下:代码红色部分提示出错。
Cannot access a disposed object.
Object name: 'frmTailInBox'.
at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous)
at System.Windows.Forms.Control.Invoke(Delegate method, Object[] args)
at CoatWareHouseManage.ControlExtensions.InvokeExAction[T,TPrams1](T this, Action`1 action, TPrams1 p1) in C:\Documents and Settings\Administrator\桌面\WenHuaDownCoat\Branches\CoatWareHouseManage-branch\Common\ControlExtensions.cs:line 51
at CoatWareHouseManage.frmTailInBox.Reader2_OnReadEpc_6C_800(Object sender, ReceiveEventArgs e) in C:\Documents and Settings\Administrator\桌面\WenHuaDownCoat\Branches\CoatWareHouseManage-branch\frmTailInBox.cs:line 273 2012-11-11 10:37:53

Invoke or BeginInvoke cannot be called on a control until the window handle has been created.
at System.Windows.Forms.Control.WaitForWaitHandle(WaitHandle waitHandle)
at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous)
at System.Windows.Forms.Control.Invoke(Delegate method, Object[] args)
at CoatWareHouseManage.ControlExtensions.InvokeExAction[T,TPrams1](T this, Action`1 action, TPrams1 p1) in C:\Documents and Settings\Administrator\桌面\WenHuaDownCoat\Branches\CoatWareHouseManage-branch\Common\ControlExtensions.cs:line 51
at CoatWareHouseManage.frmTailInBox.Reader2_OnReadEpc_6C_800(Object sender, ReceiveEventArgs e) in C:\Documents and Settings\Administrator\桌面\WenHuaDownCoat\Branches\CoatWareHouseManage-branch\frmTailInBox.cs:line 273 2012-11-11 10:37:56
...全文
510 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
cs2582030451 2012-11-14
  • 打赏
  • 举报
回复
没人... ShowCoatInfoInDgv放在Form_load中 不建议放在构造函数里面

111,096

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧