zoukankan      html  css  js  c++  java
  • 关于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);
            }
        }
    }
    View Code
  • 相关阅读:
    SSH框架搭建全过程详解
    SpringMVC项目配置全过程详解
    <抽奖奇遇>
    extJS--尚
    CentOS 7.3安装指南
    javaWeb项目之图书管理系统(附视频讲解)
    Swing记事本项目
    五、PTA实验作业(结构体)
    四、PTA实验作业(指针)
    三、PTA实验作业(数组)
  • 原文地址:https://www.cnblogs.com/wdfrog/p/3955410.html
Copyright © 2011-2022 走看看