今天code的时候碰到一个问题,记录一下:
主线程在调用一个异步的操作时,需要这个异步操作进入某个特定状态,主线程才可以继续往下执行。
(此时,并不是指这个异步操作执行完毕,因为再异步操作时会调用回调函数,不停给主线程返回数据。我不是我想说的重点,就不罗嗦了... ... )
此时,可用如下操作:
ManualResetEvent operationCompletionEvent = new ManualResetEvent(false); EventHandler<OperationStateEventArgs> OnOperationStateChange = new EventHandler<OperationStateEventArgs>(delegate(object obj, OperationStateEventArgs args) { if (args.OperationStateInfo.State == OperationState.Stopped) operationCompletionEvent.Set(); }); operation.StateChanged += OnOperationStateChange;
然后,调用该异步操作后,加一句 operationCompletionEvent.WaitOne();
"ManualResetEvent"是多线程时常用的事件操作。
PS:这是小弟的第一篇博客,时间关系也没有好好整理,希望可以坚持下去。。。。。。