C#异步

using System;
using System.Threading;
using System.Runtime.Remoting.Messaging;
using System.Collections.Generic;

namespace PurchaseSystem.Component
{
    public enum FlowType
    {
        CaiGou,
        YingFu
    }

    public delegate void ProccessFlowDelegate(FlowType type);

    public class Flow
    {
        private void AsyncFlow(FlowType type, Action<IAsyncResult, bool> callback = null)
        {
            ProccessFlowDelegate asyncFlow = AsyncFlowHandler;

            IAsyncResult asyncResult = asyncFlow.BeginInvoke(type, AsyncFlowCallback, null);

            ThreadPool.RegisterWaitForSingleObject(asyncResult.AsyncWaitHandle,
                AsyncFlowCompleteCallback, new List<object>() { asyncResult, callback },
                1800000, true);
        }

        private static void AsyncFlowCompleteCallback(object state, bool isTimeout)
        {
            List<object> stateList = state as List<object>;
            IAsyncResult result = stateList[0] as IAsyncResult;
            Action<IAsyncResult, bool> callback = stateList[1] as Action<IAsyncResult, bool>;

            if (isTimeout)
            {
                if (!result.IsCompleted)
                {
                    result.AsyncWaitHandle.Close();
                }
            }
            callback(result, isTimeout);
        }

        private static void AsyncFlowCallback(IAsyncResult result)
        {
            ProccessFlowDelegate d = (ProccessFlowDelegate)((AsyncResult)result).AsyncDelegate;
            d.EndInvoke(result);
        }

        private void AsyncFlowHandler(FlowType type)
        {
            if (type == FlowType.CaiGou)
            {

            }
            else if (type == FlowType.YingFu)
            {

            }
        }

        public void CaiGouProccess(Guid purchaseApplyId)
        {
            AsyncFlow(FlowType.CaiGou, null);
        }
    }
}
using System;
using System.Threading;
using System.Collections.Generic;
using System.Runtime.Remoting.Messaging;

namespace PurchaseSystem.Component
{
    public enum FlowProcessType 
    {
        CaiGou,
        YingFu
    }

    public delegate void FlowProcessDelegate(FlowProcessType type, params object[] parameters);

    public class FlowProcess
    {
        private void AsyncFlowProcess(FlowProcessType type, params object[] parameters)
        {
            FlowProcessDelegate asyncMailNotice = AsyncFlowProcessHandler;
            IAsyncResult asyncResult = asyncMailNotice.BeginInvoke(type, parameters, FlowProcessCallback, null);
            ThreadPool.RegisterWaitForSingleObject(
              asyncResult.AsyncWaitHandle,
              FlowProcessCompleteCallback,
              new List<object>() { asyncResult },
              1800000,
              true);
        }
        private void FlowProcessCallback(IAsyncResult result)
        {
            MailNoticeDelegate d = (MailNoticeDelegate)((AsyncResult)result).AsyncDelegate;
            d.EndInvoke(result);
        }
        private void FlowProcessCompleteCallback(object state, bool isTimeout)
        {
            List<object> stateList = state as List<object>;
            IAsyncResult result = stateList[0] as IAsyncResult;
            if (isTimeout)
            {
                if (!result.IsCompleted)
                {
                    result.AsyncWaitHandle.Close();
                }
            }
        }
        private void AsyncFlowProcessHandler(FlowProcessType type, params object[] parameters)
        {
            if (type == FlowProcessType.CaiGou)
            {


            }
        }

        public void PurchaseApplyApprovalMailNotice2(Guid purchaseApplyId)
        {
            AsyncFlowProcess(FlowProcessType.CaiGou, purchaseApplyId);
        }
    }
}

 

posted @ 2015-04-15 18:07  一千零一夜  阅读(259)  评论(0编辑  收藏  举报