首页 > 其他 > 详细

异步编程设计模式Demo - AsyncComponentSample

时间:2015-05-05 16:04:01      阅读:363      评论:0      收藏:0      [点我收藏+]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Threading;

namespace AsyncProgramDemo
{
    public class AsyncComponentSample : Component
    {
        public event Action<object, BreakEventArgs> Break;

        public event Action<object, DebugCompltedEventArgs> Completed;

        public void StartDebug()
        {
            AsyncOperation asyncOp = AsyncOperationManager.CreateOperation(1);
            Action<AsyncOperation> exec = new Action<AsyncOperation>(Execute);
            exec.BeginInvoke(asyncOp, null, null);
        }

        private void Execute(AsyncOperation asyncOp)
        {
            Thread.Sleep(1000);
            SendOrPostCallback callback = new SendOrPostCallback(Callback);
            asyncOp.Post(callback, 7);
            Thread.Sleep(1000);
            SendOrPostCallback completedCallback = new SendOrPostCallback(CompltedCallback);
            asyncOp.PostOperationCompleted(completedCallback, "completed");
        }

        private void Callback(object lineNumber)
        {
            if (Break != null)
            {
                Break(this, new BreakEventArgs((int)lineNumber));
            }
        }

        private void CompltedCallback(object msg)
        {
            if (Completed != null)
            {
                Completed(this, new DebugCompltedEventArgs(msg.ToString()));
            }
        }
    }

    public class BreakEventArgs : EventArgs
    {
        public int LineNumber;

        public BreakEventArgs(int lineNumber)
        {
            this.LineNumber = lineNumber;
        }
    }

    public class DebugCompltedEventArgs : AsyncCompletedEventArgs
    {
        public string Msg;

        public DebugCompltedEventArgs(string msg) : base(null, false, null)
        {
            this.Msg = msg;
        }
    }
}

 

异步编程设计模式Demo - AsyncComponentSample

原文:http://www.cnblogs.com/chengshuiqiang/p/4479051.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!