0
在WCF项目中使用回调时遇到了一些问题。在使用WCF通过双工通道发送新请求之前,确保所有回调已完成
首先,服务器调用客户端,然后该请求转发到Windows上的一些功能Foo
窗体GUI:
GUI CLASS
delegate void DoForward();
public void ForwardToGui() {
if (this.cmdSomeButton.InvokeRequired) {
DoForward d = new DoForward(ForwardToGui);
this.Invoke(d);
}
else {
Process(); // sets result variable in callback class as soon as done
}
}
}
回调类
object _m = new object();
private int _result;
public int result {
get { return _result; }
set {
_result = value;
lock(_m) {
Monitor.PulseAll(_m);
}
}
}
[OperationContract]
public int Foo() {
result = 0;
Program.Gui.ForwardToGui();
lock(_m) {
Monitor.Wait(_m, 30000);
}
return result;
}
现在的问题是,用户应该能够取消的过程中,不正常工作:
服务器接口
[OperationContract]
void Cleanup();
GUI类
private void Gui_FormClosed(object sender, EventArgs e) {
Program.callbackclass.nextAction = -1;
// so that the monitor pulses and Foo() returns
Program.server.Cleanup();
}
的问题,这是Cleanup()
挂起。但是,当我在Process()
未运行时关闭表单时,它正常工作。
来源似乎是在监视器脉冲等之前调用Cleanup()
,因此在服务器的最后一个请求尚未响应之前,会向服务器发送新的请求。
我怎样才能解决这个问题?在致电Cleanup()
之前,我如何确保当前不执行Foo()
?
我太困惑了,试图将代码片段拼凑在一起。这两个代码片段是否在相同的GUI类中是“GUI类”? 我看到的第一个警告是你调用System.Windows.Form.Invoke而不是System.Windows.Forms.BeginInvoke。那是故意的吗? – 2010-05-26 23:24:07
用GUI类标记的两个部分都是相同的类。我正在尝试。 BeginInvoke似乎完成了这项工作!感谢:-)你介意再次发表你的评论作为答案? – Etan 2010-05-27 00:20:57