c#中的异步

c#中的异步,第1张

概述1.委托的BeginEnvoke/EndInvoke BeginEnvoke\EndInvoke是委托的异步版本。 public class AsyncFroDelegate { public delegate int AddHandler(int a, int b); public static int Add(int a, int b) { Console.WriteLine($"线程 {Th

1.委托的BeginEnvoke/EndInvoke

BeginEnvoke\EndInvoke是委托的异步版本。

public class AsyncFroDelegate { public delegate int AddHandler(int a,int b); public static int Add(int a,int b) { Console.Writeline($"线程 {Thread.CurrentThread.ManagedThreadID} in add()"); Thread.Sleep(3000); Console.Writeline("计算完成!"); return a + b; } public static voID AsyncInvoke1() { Console.Writeline("===异步调用 AsyncInvokeTest==="); AddHandler handler = new AddHandler(Add); IAsyncResult result = handler.BeginInvoke(1,2,null,null); //EndInvoke,使得主线程处于阻塞状态            Console.Writeline($"线程 {Thread.CurrentThread.ManagedThreadID} in AsyncInvoke1()"); Console.Writeline("打印EndInvoke的结果 =" + handler.EndInvoke(result)); Console.Writeline("继续做别的事情。。。"); Console.ReadKey(); } }

 

  

BeginInvoke使得CLR创建了一个新的线程去执行Add方法。此时主线程不受影响可以继续做其他事情。直到遇到EndInvoke,需要等待异步调用结果才被阻塞。如果主线程不依赖这个调用结果。可是使用回调,让主线不被阻塞。

 

   /// <summary>        /// 异步回调 /// </summary>        public static voID AsyncInoke3() {
        Console.Writeline($"线程 {Thread.CurrentThread.ManagedThreadID} in AsyncInoke3()"); AddHandler handler
= new AddHandler(Add); IAsyncResult arr = handler.BeginInvoke(1,2,myCallback,handler); Console.Writeline("主线程完成"); } private static voID myCallback(IAsyncResult ar) { Console.Writeline($"线程 {Thread.CurrentThread.ManagedThreadID} in myCallback()"); //AddHandler handler = (AddHandler)((AsyncResult)ar).AsyncDelegate; //AsyncState就是通过BeginInvoke函数的最后一个参数传递过来的的 AddHandler handler1 = (AddHandler)ar.AsyncState; //Console.Writeline("调用结果" + handler.EndInvoke(ar)); Console.Writeline("调用结果" + handler1.EndInvoke(ar)); }

2.Thread


public Thread(ThreadStart start); public Thread(ParameterizedThreadStart start); public Thread(ThreadStart start,int maxStackSize); public Thread(ParameterizedThreadStart start,int maxStackSize);
ThreadStart 无参数,无返回值的委托
ParameterizedThreadStart 带一个参数,无返回值的委托
maxStackSize 线程要使用的堆栈的大小,默认1M。
 public class AsyncForThread { public static voID ClIEnt() { Thread thread1 = new Thread(Print); thread1.Start(); Thread thread2 = new Thread(PrintEx); thread2.Start("test"); } private static voID PrintEx(object content) { int threadID = Thread.CurrentThread.ManagedThreadID; Console.Writeline($"当前线程ID:{threadID}\t{content}"); } private static voID Print() { int threadID = Thread.CurrentThread.ManagedThreadID; Console.Writeline($"当前线程ID:{threadID} 无参数"); } }

 

3.ThreadPool

在面向对象的编程中,创建和销毁对象是很费事的,因为创建一个对象要获取内存资源或者其他更多的资源。.Net Framework已经为我们提供了一个“线程池””供使用。

需要注意的是,线程池中的线程均为后台线程,即他们的IsBAckground属性为true,这意味着在所有的前台线程都已退出后,ThreadPool中的线程不会让应用程序继续保持运行。

使用线程池的一些限制:

当需要创建一个前台线程时不应该使用线程池@H_338_403@ 无法设置线程优先级@H_338_403@ 执行任务是无法选择具体线程@H_338_403@ 执行时间过长@H_338_403@
public class AsyncForThreadPool    {        public static voID ClIEnt()        {            ThreadPool.QueueUserWorkItem(Counter);            ThreadPool.QueueUserWorkItem(Counter,"test");            Console.Writeline($"[线程ID={Thread.CurrentThread.ManagedThreadID}]主线程启动。");        }        private static voID Counter(object state)        {            //throw new NotImplementedException();            for (int i = 0; i < 5; i++)            {                Thread.Sleep(50);                if (state != null)                {                    Console.Writeline($"[线程ID:{Thread.CurrentThread.ManagedThreadID}] {state}");                }                else                {                    Console.Writeline($"[线程ID:{Thread.CurrentThread.ManagedThreadID}] {i.ToString()}");                }            }        }    }

4.Task

由于使用ThreadPool无法获取到并发完成时的返回值。引入了Task。

 public static voID ClIEnt() { var parent = new Task(() => { CancellationTokenSource cts = new CancellationTokenSource(); //TaskCreationoptions.Attachedtoparent 该创建的任务为子任务 //TaskContinuationoptions.ExecuteSynchronously 创建的延续任务为同步执行                var tf = new TaskFactory(cts.Token,TaskCreationoptions.Attachedtoparent,TaskContinuationoptions.ExecuteSynchronously,TaskScheduler.Default); //创建三个子任务                var childTasks = new[] { tf.StartNew(()=>Sum(cts.Token,50)),tf.StartNew(()=>Sum(cts.Token,100)),int.MaxValue)) }; //任何子任务异常则取消所有其他子任务               for(int i=0;i<childTasks.Length;i++) { Console.Writeline(childTasks[i].ID); childTasks[i].ContinueWith(t => { Console.Writeline("cancelled is :"+t.ID);cts.Cancel(); },TaskContinuationoptions.OnlyOnFaulted); }; //所有子任务抛出异常,从未出错、未取消的任务获取返回的最大值, //然后将最大值由另一个任务来显示                tf.ContinueWhenAll(childTasks,tasks => tasks.Where(t => !t.IsFaulted && !t.IsCanceled).Max(t => t.Result),CancellationToken.None) .ContinueWith(t => Console.Writeline("The Maximum is :" + t.Result),TaskContinuationoptions.ExecuteSynchronously); }); parent.ContinueWith(p => { StringBuilder sb = new StringBuilder("the follwing exceptins(s) occurred:" + Environment.Newline); foreach (var e in p.Exception.Flatten().InnerExceptions) { sb.Appendline(" " + e.GetType().ToString()); Console.Writeline(sb.ToString()); } },TaskContinuationoptions.OnlyOnFaulted); parent.Start(); } private static int Sum(CancellationToken ct,int n) { int result = 0; for (int i = 0; i <= n;i++) { ct.ThrowIfCancellationRequested(); checked { result += i; } } return result; }

 

 

5.async/await

这两个词比较有迷惑性。目前从实验得出来的结论是:

主线程执行路径:1-->3-->5 后面遇到await 关键字会立即返回主调函数紧接着到2的位置。

任务t1 开始执行位置4,然后去执行一堆“等着”他的代码,那就是await关键字之后的代码,像极了“回调”。

 public static voID TaskRun5()        {            ShowThread(1);            test();            ShowThread(2);        }        private async static Task test()        {            ShowThread(3);            Task<string> t1 = Task.Run(()=> {                Thread.Sleep(1000);                ShowThread(4);                return "a";            });            ShowThread(5);            Console.Writeline(await t1);            ShowThread(6);        }       static voID ShowThread(int pos)        {                Console.Writeline($"位置[{pos}]线程:"+Thread.CurrentThread.ManagedThreadID);        }

  

总结

以上是内存溢出为你收集整理的c#中的异步全部内容,希望文章能够帮你解决c#中的异步所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/langs/1214208.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存