用Reactive Extension实现异步方法超时的操作

            Action action = new Action(() =>
                {
                    Thread.Sleep(5000);
                });

            var Async = Observable.FromAsyncPattern(action.BeginInvoke, action.EndInvoke);
            Observable.Timeout(Async.Invoke(), DateTimeOffset.Now.AddSeconds(3)).ObserveOnDispatcher().Subscribe(it =>
                {
                    MessageBox.Show("执行完毕");
                }, ex =>
                    {
                        MessageBox.Show("超时");
                    });
            
posted @ 2012-07-10 15:49  therockthe  阅读(350)  评论(0编辑  收藏  举报