【发布时间】:2014-05-17 03:56:57
【问题描述】:
这是来自 Microsoft 的代码 sn-p。 我对异步方法调用有疑问。
因为我们在 Begin-invoke 之后调用 end.Invoke,所以看起来我们在进行同步调用。因为我们在等待异步调用的返回值。
如果在我们调用 end.invoke 时异步方法没有完成会发生什么。 我们可以继续下一个语句还是我们必须等待。
如果这发生在多线程环境中,他们如何处理回调信号以纠正线程。
public void DemoEndInvoke()
{
MethodDelegate dlgt = new MethodDelegate (this.LongRunningMethod) ;
string s ;
int iExecThread;
// Initiate the asynchronous call.
IAsyncResult ar = dlgt.BeginInvoke(3000, out iExecThread, null, null);
// Do some useful work here. This would be work you want to have
// run at the same time as the asynchronous call.
// Retrieve the results of the asynchronous call.
s = dlgt.EndInvoke (out iExecThread, ar) ;
MessageBox.Show (string.Format ("The delegate call returned the string: \"{0}\",
and the number {1}", s, iExecThread.ToString() ) );
}
【问题讨论】:
标签: c# multithreading asynchronous .net-3.5 begininvoke