码迷,mamicode.com
首页 > Windows程序 > 详细

C#异步

时间:2015-04-15 19:21:26      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:

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);
        }
    }
}

 

C#异步

标签:

原文地址:http://www.cnblogs.com/daixingqing/p/4429346.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!