zoukankan      html  css  js  c++  java
  • 异步编程设计模式Demo

    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;
            }
        }
    }
  • 相关阅读:
    移动端 h5开发相关内容总结——CSS篇
    水滴导航特效
    腾讯课堂之前端开发html5css3javascriptjQueryJS年薪20万
    阿里前端笔试总结
    Web 开发的未来:React、Falcor 和 ES6
    Yii2按需加载图片怎么做?
    Yii2 灵活加载js、css
    Ubuntu上搭建SVN
    yii pageTitle与Yii::app()->name的区别
    Mysql--Database Exception (#42) 数据库错误
  • 原文地址:https://www.cnblogs.com/chengshuiqiang/p/4479051.html
Copyright © 2011-2022 走看看