zoukankan      html  css  js  c++  java
  • C#异步编程

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    namespace ConsoleApplication1
    {
        class Program
        {
            private static int Task(int ms)
            {
                Console.WriteLine("任务开始");
                Thread.Sleep(ms);
                Random random = new Random();
                int n = random.Next(10000);
                Console.WriteLine("任务完成");
                return n;
            }
            private delegate int NewTaskDelegate(int ms);
            static void Main(string[] args)
            {
                NewTaskDelegate task = Task;
                IAsyncResult asyncResult = task.BeginInvoke(2000, null, null);  //第一个参数传递给调用方法
                //EndInvoke方法将被阻隔2秒
                /*while (!asyncResult.AsyncWaitHandle.WaitOne(100, false))
                {
                    Console.Write("*");
                }*/
                //EndInvoke方法将被阻隔2秒
                while (!asyncResult.IsCompleted)
                {
                    Console.Write("*");
                    Thread.Sleep(100);
                }
                int result = task.EndInvoke(asyncResult);
                Console.WriteLine(result);
            }
        }
    }

  • 相关阅读:
    JDom写入XML例子
    hdu 2549
    hdu 1328
    hdu 1334
    hdu 2547
    hdu 2374
    hdu 2550
    hdu 1335
    hdu 2548
    hdu 1722
  • 原文地址:https://www.cnblogs.com/zztong/p/6695207.html
Copyright © 2011-2022 走看看