我在客户端应用程序主界面用到了多线程侦听消息,但出错:
未处理InvalidOperationException,Undo 操作遇到饿上下文与相应Set 操作中应用的上下文不同.可是因为在线程上Set了一个上下文但未进行还原(撤消).
//主窗体
public frmMain1(int subSystemID, string subSystemName)
{
this._IsNeedReg = true;
this.InitializeComponent();
this._frmMain = new frmMain();
ClientApp.MainForm = this;
// this.frmMain1_SizeChanged(null, null);
this.InitSubSystemMenu();
this.InitMenu(subSystemID, subSystemName);
this.InitFavouriteMenu();
try
{
WinUI.MessageClient.AssignMessage sms_email_client = new WinUI.MessageClient.AssignMessage();
MessageClient.SendIntantMsgClient messageClient = new WinUI.MessageClient.SendIntantMsgClient(ClientApp.CurrentUser.Employee.EmployeeKeyID);
ClientApp.InstantMessageClient = messageClient;
ClientApp.SMS_Emai_Client = sms_email_client;
ExecutionContext eC = ExecutionContext.Capture();
AsyncFlowControl afc = ExecutionContext.SuppressFlow();
Thread startTd = new Thread(new ThreadStart(ClientApp.InstantMessageClient.StartListenMessage));
startTd.Start();
//startTd.Join();
afc.Undo(); //出错后加上去,也不能解决
}
catch(Exception ex)
{
// ExceptionDealCenter.Deal("系统加载失败",
// ex, ClientApp.CurrentUser,
// "加载主窗口");
throw ex;
}
HomePage page1 = new HomePage();
page1.MdiParent = this;
page1.WindowState = FormWindowState.Maximized;
page1.Show();
ClientApp.HomePage = page1;
}
/// <summary>
/// 侦听其他客户端发送消息
/// </summary>
public void StartListenMessage()
{
if(dealOnServer == null) return ;
AsyncFlowControl afc = ExecutionContext.SuppressFlow();
callback = new MyCallbackClass() ;
//开始监听消息
dealOnServer.GetMesssageFromServer += new GetMessageFromServerEventHander(callback.SubmissionCallback);//调用服务器端代码,弹出消息窗口
afc.Undo();
}
//点击消息窗口上面的链接
private void LinkCondition()
{
showAssociate = new ShowAssociateHandler(Associate.ShowAssociate);
}
private void ShowLink()
{
object[] obj ={_InstantMessage.link};
LinkCondition();
this.Invoke(showAssociate,obj);
}
private void Title_linkLabel_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
{
try
{
if (_InstantMessage.link.UseModuleID == string.Empty) return;
AsyncFlowControl afc = ExecutionContext.SuppressFlow();
Thread Td = new Thread(new ThreadStart(this.ShowLink));
Td.Start();
afc.Undo();
}
catch (Exception ex)
{ throw ex; }
}
/// <summary>
/// 深化/链接到目标模块
/// </summary>
/// <param name="link">条件表达式</param>
public static void ShowAssociate(systemFrameWork.Message.Link link)
{
switch(link.ExecuteType)
{
case systemFrameWork.Message.ExecuteType.Show:
DataList.Container dc1 = (new DataList.Container(link.UseModuleID));
dc1.MdiParent = ClientApp.MainForm;
dc1.ShowBrowser();
break;
case systemFrameWork.Message.ExecuteType.ShowAndEdit:
string keyid = link.Condition.Substring(link.Condition.IndexOf("=") + 1);
frmTableEditor fte = (new frmTableEditor("", link.UseModuleID, null, null));
fte.MdiParent = ClientApp.MainForm;
fte.Open(keyid);
break;
case systemFrameWork.Message.ExecuteType.ShowAndList:
string sqlwhere = " and " + link.Condition.Trim();
DataList.Container dc = (new DataList.Container(link.UseModuleID));
dc.MdiParent = ClientApp.MainForm;
dc.ShowBrowser(sqlwhere);
break;
}
}
注:
AsyncFlowControl afc = ExecutionContext.SuppressFlow();
afc.Undo();
上面两行代码是出错后加上去的,还是会报错;
当某客户端发送消息后另一客户端接收到消息,并弹出消息窗口,这时候点击消息窗口上面的链接转到消息列表时出现上述错误;
新问题:{"在某个线程上创建的控件不能成为在另一个线程上创建的控件的父级。\r\n参数名: value"};
望高手指点!谢谢!
[此贴子已经被作者于2007-6-27 17:47:21编辑过]