zoukankan      html  css  js  c++  java
  • 多线程17-Async Programming Model

        class Program
        {
            private delegate string RunOnThreadPool(out int threadId);
            private static void Callback(IAsyncResult ar)
            {
                Console.WriteLine("Starting a callback");
                Console.WriteLine("State passed to a callback:{0}", ar.AsyncState);
                Console.WriteLine("Is Thread Polll Thread={0}", Thread.CurrentThread.IsThreadPoolThread);
                Console.WriteLine("Thread pool worker thread id :{0}", Thread.CurrentThread.ManagedThreadId);
            }
            private static string Test(out int ThreadId)
            {
                Console.WriteLine("Starting ....");
                Console.WriteLine("Is thread pool thread:{0}", Thread.CurrentThread.IsThreadPoolThread);
                Thread.Sleep(2);
                ThreadId = Thread.CurrentThread.ManagedThreadId;
                return string.Format("Thread pool worker therad id was ={0}", ThreadId);
            }
            static void Main()
            {
                int threadId = 0;
                RunOnThreadPool poolDelegate = Test;
                var t = new Thread(() => Test(out threadId));
                t.Start();
                t.Join();
                Console.WriteLine("Thread id:{0}", threadId);
                IAsyncResult r = poolDelegate.BeginInvoke(out threadId, Callback, "a delegae asyncResult call");
                string result = poolDelegate.EndInvoke(out threadId, r);
                Console.WriteLine("Thread pool worker thread id:{0}", threadId);
                Console.WriteLine(result);
                Thread.Sleep(TimeSpan.FromSeconds(2));
            }
        }
  • 相关阅读:
    js中return false,return,return true的用法及区别
    C#中关于页面缓存
    项目运行时出现webconfig="machineApplication......."之类的错误
    后台页面无法找到前台页面控件的ID
    .NET对文件的多种操作
    前台JS代码向后台传递参数
    多条件查询的Gridview分页显示
    C#中对数据做视图处理RowFilter
    JavaScript自触发时的参数传递
    Sys.WebForms.PageRequestManagerTimeoutException:服务器请求超时
  • 原文地址:https://www.cnblogs.com/shidengyun/p/5609375.html
Copyright © 2011-2022 走看看