首页 新闻 搜索 专区 学院

使用AsyncOperation类遇到的问题

0
悬赏园豆:50 [已关闭问题] 关闭于 2012-02-09 14:43

我定义了一个类,在类中实现异步的操作,有一个异步方法。定义了一个AsyncOperation变量,然后在开始异步方法的时候创建AsyncOperation,但是在执行异步以后,类调用AsyncOperation变量就是null,而在异步操作的方法内部却不是null,我想实现异步的取消操作。请问这个是什么原因?

  1     public class AsynTest
2 {
3 private HybridDictionary userStateToLifetime = new HybridDictionary();//创建区分大小写的空白
4 public StateChangedEventHandler StateChanged;
5
6 AsyncOperation asyncOp;
7
8 private string classid = "TT";
9
10 //AsyncOperation 跟踪异步进度的生存期
11
12 RunDelegate s;
13 IAsyncResult rsult;
14
15 public void RunTest(object args)
16 {
17 asyncOp = AsyncOperationManager.CreateOperation(classid);
18 lock (userStateToLifetime.SyncRoot)
19 {
20 if (userStateToLifetime.Contains(classid))
21 {
22 throw new ArgumentException("userState parameter must be unique", "userState");
23 }
24 userStateToLifetime[classid] = asyncOp;
25 }
26 s = new RunDelegate(Write);
27 rsult = s.BeginInvoke(asyncOp, null, args);
28 }
29
30 ///<summary>
31 /// 取消操作
32 ///</summary>
33 ///<param name="userState"></param>
34 public void CancelAsyn()
35 {
36 if (this.asyncOp != null)
37 {
38 lock (userStateToLifetime.SyncRoot)
39 {
40 userStateToLifetime.Remove(asyncOp.UserSuppliedState);
41 }
42 }
43 }
44
45 private bool IsCanceled(object userstate)
46 {
47 return (userStateToLifetime[userstate] == null);
48 }
49
50 ///<summary>
51 /// 内部方法,此方法只传递外部的参数变量,这个耗时的操作方法
52 ///</summary>
53 ///<param name="state"></param>
54 private void Write(object args)
55 {
56 StateChangeArgs e = new StateChangeArgs();
57
58 if (IsCanceled(asyncOp.UserSuppliedState))
59 {
60 e.State = "取消操作";
61 e.WorkState = WorkState.Canceled;
62 this.PostState(e, false);
63 return;
64 }
65 else
66 {
67 System.Threading.Thread.Sleep(3 * 1000);
68 e.State = "正在验证服务器";
69 e.WorkState = WorkState.Running;
70 this.PostState(e, false);
71 }
72
73 //向外部发布操作的状态信息
74 e = new StateChangeArgs();
75 if (IsCanceled(asyncOp.UserSuppliedState))
76 {
77 e.State = "取消操作";
78 e.WorkState = WorkState.Canceled;
79 this.PostState(e, false);
80 return;
81 }
82 else
83 {
84 System.Threading.Thread.Sleep(5 * 1000);
85 e.State = "正在发送邮件";
86 e.WorkState = WorkState.Running;
87 this.PostState(e, false);
88 }
89
90 for (int i = 0; i < 20; i++)
91 {
92 e = new StateChangeArgs();
93 if (IsCanceled(asyncOp.UserSuppliedState))
94 {
95 e.State = "取消操作";
96 e.WorkState = WorkState.Canceled;
97 this.PostState(e, false);
98 return;
99 }
100 else
101 {
102 System.Threading.Thread.Sleep(500);
103 e.State = "已发送" + (i + 1) + "字节";
104 e.WorkState = WorkState.Running;
105 this.PostState(e, false);
106 }
107 }
108
109 if (IsCanceled(asyncOp.UserSuppliedState))
110 {
111 e.State = "取消操作";
112 e.WorkState = WorkState.Canceled;
113 this.PostState(e, false);
114 return;
115 }
116 else
117 {
118 e = new StateChangeArgs();
119 e.State = "发送完成";
120 e.WorkState = WorkState.Completed;
121 this.PostState(e, true);
122 }
123 }
124
125 //向外部发送状态通知
126 private void PostState(StateChangeArgs args, bool complete)
127 {
128 if (complete)
129 {
130 asyncOp.PostOperationCompleted(new SendOrPostCallback(_statechange), args);
131 //asyncOp.OperationCompleted();//结束异步操作的生命期
132 }
133 else
134 {
135 asyncOp.Post(new SendOrPostCallback(_statechange), args);
136 }
137 }
138
139 private void _statechange(object arg)
140 {
141 StateChangeArgs e = arg as StateChangeArgs;
142 OnChangeState(e);
143 }
144
145 private void OnChangeState(StateChangeArgs e)
146 {
147 if (e.WorkState == WorkState.Completed)
148 {
149 s.EndInvoke(rsult);
150 }
151 if (this.StateChanged != null)
152 {
153 this.StateChanged(e);
154 }
155 }
156
157 }
158
159 public class StateChangeArgs
160 {
161 private string _state;
162 public string State
163 {
164 get { return _state; }
165 set { _state = value; }
166 }
167
168 private WorkState _workstate;
169 public WorkState WorkState
170 {
171 get { return _workstate; }
172 set { _workstate = value; }
173 }
174 }
175
176 public enum WorkState
177 {
178 Waiting=0,
179 Start = 1,
180 Running = 2,
181 Completed = 3,
182 Canceled = 4
183 }
184
185 public delegate void RunDelegate(AsyncOperation asyncOp);
186 public delegate void CallbackResultHandler(IAsyncResult asyrest);
187 public delegate void StateChangedEventHandler(StateChangeArgs e);
Longkin的主页 Longkin | 初学一级 | 园豆:3
提问于:2012-02-08 10:18
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册