关于InvokeMethod Activity的异步调用
讨论地址:http://www.cnblogs.com/foundation/archive/2009/12/17/1626617.html
结论是IsCompleted的设置被忽略,看代码里注释
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DelayAndThreadWorkflow { public class myAsyncResult : IAsyncResult { public object AsyncState { get; set; } public System.Threading.WaitHandle AsyncWaitHandle { get; set; } public bool CompletedSynchronously { get; set; } public bool IsCompleted { get; set; } public myAsyncResult() { IsCompleted = false; CompletedSynchronously = false; } } public class threadMethod { AsyncCallback callback; IAsyncResult asyncResult; int n; int result; public void myCall(int n) { for (int i = 1; i <= n; i++) { result = result + i; } System.Console.WriteLine("myCall,result:{0}", result); } #region /* 1。如果myAsyncResult.CompletedSynchronously=true指示是同步完成的方法 * 那么WFRuntime就没必要判断myAsyncResult.IsCompleted,会在完成BeginmyCall后马上掉用 * EndmyCall方法 2。如果myAsyncResult.CompletedSynchronously=fasle * 那么WFRuntime只会根据会在activity回调callback时调用EndmyCall,同样忽略IsCompleted的设置 */ #endregion public IAsyncResult BeginmyCall(int n, AsyncCallback callback, object asyncState) { System.Console.WriteLine("BeginmyCall,n:{0}", n); this.n = n; this.callback = callback; this.asyncResult = new myAsyncResult() { AsyncState = asyncState,IsCompleted=false }; System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(myProcessThread)); thread.Start(); return this.asyncResult; } public void EndmyCall(IAsyncResult r) { Console.WriteLine("EndmyCall,result:{0}", result); } public void myProcessThread() { Console.WriteLine("myProcessThread"); for (int i = 1; i <= n; i++) { result = result + i; System.Console.WriteLine(i); System.Threading.Thread.Sleep(500); } //(asyncResult as myAsyncResult).IsCompleted = true; callback(asyncResult); } } }