zoukankan      html  css  js  c++  java
  • 异步调用

    因为程序运行速度比较慢,所以要把长时间运行的代码进行异步处理。所以做了一个如下的测试:

    Using directives

    namespace testASycCall
    {
        
    public delegate void AsyncCaller();
        
    class Program
        
    {
            
    void Call0()
            
    {
                Console.WriteLine(
    "Call0:Begin");
                System.Threading.Thread.Sleep(
    2000);
                Console.WriteLine(
    "Call0:End");
            }


            
    void Call1()
            
    {
                Console.WriteLine(
    "Call1:Begin");
                System.Threading.Thread.Sleep(
    2000);
                Console.WriteLine(
    "Call1:End");
            }



            
    static void Main(string[] args)
            
    {
                Program p 
    = new Program();
                Console.WriteLine(
    "ready async call0");
                AsyncCaller ac 
    = new AsyncCaller(p.Call0);
                Console.WriteLine(
    "invoke async call0");
                IAsyncResult ar 
    = ac.BeginInvoke(nullnull);
                Console.WriteLine(
    "call call1");
                p.Call1();
                Console.WriteLine(
    "call1 end");
                Console.WriteLine(
    "wait call 0 end");
                ar.AsyncWaitHandle.WaitOne();
                Console.WriteLine(
    "call 0 end");
            }

        }

    }


    程序运行结果如下:

    ready async call0
    invoke async call0
    call call1
    Call0:Begin
    Call1:Begin
    Call0:End
    Call1:End
    call1 end
    wait call 0 end
    call 0 end

    看起来并行的还比较明显!

    相同的例子在MSDN上面也有,如果你机器上装了MSDN的话,可以到这里看到原文。不过俺的例子好像比它的更明显些

  • 相关阅读:
    Oracle 12c中文乱码,修改字符集的方法
    C++设计模式——简单工厂模式与策略模式比较
    C++设计模式——工厂模式Factory Method
    JavaWeb——Servlet基础
    C++设计模式——装饰模式Bridge-Pattern
    线性代数思维导图(2)——矩阵
    线性代数思维导图(1)——行列式
    C++设计模式——适配器模式Bridge-Pattern
    C++设计模式——桥接模式Bridge-Pattern
    不想写博客?那试试日记吧!
  • 原文地址:https://www.cnblogs.com/BigTall/p/63945.html
Copyright © 2011-2022 走看看