随笔- 672
文章- 2
评论- 325
阅读-
281万
随笔分类 - C#多线程
.NET中的线程池ThreadPool(链接)
摘要:微软推荐在.NET中使用多线程开发时,都使用线程池,下面这篇微软文档介绍了.NET中的线程池类ThreadPool: ThreadPool Class 注意上面文档中的这句话: There is one thread pool per process. 也就是说,每个.NET进程(process)中
阅读全文
.NET项目AsyncLocal在链路追踪中的应用(转载)
摘要:目录 前言 老传统做法 AspNetCore的TraceIdentifier AsyncLocal在链路追踪的应用 项目应用 AspNetCore 前言 在项目生产中日志的记录是必不可少的,在.net项目中,要说日志组件,log4net绝对可有一席之地,随着公司业务的发展,微服务则必定无可避免。在跨
阅读全文
Task.Result, Task.Wait(), Task.WaitAll(), Task.WaitAny()都会抛出AggregateException异常(链接)
摘要:下面几篇文章阐述了,当Task在运行过程中发生了未处理异常时,在调用Task.Result, Task.Wait(), Task.WaitAll(), Task.WaitAny()时,都会抛出AggregateException异常。 下面的文章章节,阐述了如何在调用Task.Wait(),Task
阅读全文
IDisposable和IAsyncDisposable接口(链接)
摘要:关于IDisposable接口,我相信大家都很熟悉了,下面是微软官方文档的介绍: IDisposable Interface Implement a Dispose method 在C# 8.0中,微软引入了IAsyncDisposable接口,下面是微软官方文档的介绍: IAsyncDisposa
阅读全文
C# 8中使用IAsyncEnumerable<T>和IAsyncEnumerator<T>,来实现异步迭代(链接)
摘要:首先,我们需要知道为什么我们要使用IAsyncEnumerable<T>和IAsyncEnumerator<T>来实现异步迭代,它有什么好处?可以参考下面这篇文章: IAsyncEnumerable In C# 8 其次,关于异步迭代的实现原理和细节,可以参考下面这篇文章: Iterating wi
阅读全文
C# ValueTask相关文章(链接)
摘要:微软官方文档介绍: ValueTask<TResult> Struct ValueTask Struct 为什么我们需要使用ValueTask,可以参考下面几篇文章: Why would one use Task<T> over ValueTask<T> in C#? Understanding t
阅读全文
Async/Await模式中使用TransactionScope时,要设置参数为TransactionScopeAsyncFlowOption.Enabled枚举(转载)
摘要:TransactionScope and Async/Await. Be one with the flow! You might not know this, but the 4.5.0 version of the .NET Framework contains a serious bug re
阅读全文
Is default(CancellationToken) equivalent to CancellationToken.None?(转载)
摘要:问 Looking at the implementation of CancellationToken.None, it is simply returning default(CancellationToken). However, I see no reference in Cancellat
阅读全文
Do I need to dispose of Tasks?(链接)
摘要:Do I need to dispose of Tasks? Here’s my short answer to this question: “No. Don’t bother disposing of your tasks.”
阅读全文
What is the use for Task.FromResult<TResult> in C#(转载)
摘要:问: In C# and TPL (Task Parallel Library), the Task class represents an ongoing work that produces a value of type T.I'd like to know what is the need
阅读全文
await Task.Yield()和await Task.CompletedTask有什么不同
摘要:有时候我们在代码中要执行一些非常耗时的操作,我们不希望这些操作阻塞调用线程(主线程)的执行,因为调用线程(主线程)可能还有更重要的工作要做,我们希望将这些非常耗时的操作由另外一个线程去执行,这个时候就可以用到await Task.Yield(),它借助了C# 5.0中的异步函数关键字await as
阅读全文
Whats the benefit of passing a CancellationToken as a parameter to Task.Run?(转载)
摘要:问: Obviously I realize it enables me to cancel the task, but this code achieves the same effect without having to pass the token into Task.RunWhat is
阅读全文
C#: Thread.Sleep vs. Task.Delay (转载)
摘要:We can use both Thread.Sleep() and Task.Delay() to suspend the execution of a program (thread) for a given timespan. But are we actually suspending th
阅读全文
C#中如何使用异步lambda表达式来初始化委托实例
摘要:下面我们通过一个.NET Core控制台项目,来展示如何使用异步lambda表达式来初始化三种委托实例:Func<Task<TResult>>、Func<Task>、Action 我们还展示了如何将Main函数改造为异步函数。 希望上面的例子对大家有所帮助~
阅读全文
Why use async requests instead of using a larger threadpool?(转载)
摘要:问: During the Techdays here in the Netherlands Steve Sanderson gave a presentation about C#5, ASP.NET MVC 4, and asynchronous Web. He explained that w
阅读全文
System.Threading.Timer如何正确地被Dispose
摘要:System.Threading.Timer是.NET中一个定时触发事件处理方法的类(本文后面简称Timer),它背后依靠的是.NET的线程池(ThreadPool),所以当Timer在短时间内触发了过多的事件处理方法后,可能会造成事件处理方法在线程池(ThreadPool)中排队,可以参考这篇文章
阅读全文
C#夯实基础之多线程二:主线程、前台线程与后台线程(转载)
摘要:我们在《C#夯实基础之多线程一:初识多线程》一文中第二部分中指出,既然windows最终发展出了多线程模型,按理说,我们直接使用一个.NetFramework的线程类就可以直接撸代码了,但在这之前,我们还需要认识一下线程的一些基本特性,它们的出现并不是多余的,而是为了解决一部分问题出现的,毕竟存在即
阅读全文
C#多线程中的异常处理(转载)
摘要:常规Thread中处理异常 使用Thread创建的子线程,需要在委托中捕捉,无法在上下文线程中捕捉 Task中处理异常 1.仍然可以在委托中捕获异常2.可以捕获Task.Wait() 或者 Task.Result 的 AggregateException 异常 AggregateException
阅读全文
c#中的多线程异常 (转载)
摘要:1.对于Thread操作的异常处理 在DoWork函数里抛出的异常时不会被主线程的try,catch捕捉的,各个线程应该有自己的try,catch去处理线程异常。正确写法: 2. 异步函数的异常处理例如如 WebClient中的 UploadStringAsync,它的异常会在UploadStrin
阅读全文
关于C#中async/await中的异常处理(下)-(转载)
摘要:上一篇文章里我们讨论了某些async/await的用法中出现遗漏异常的情况,并且谈到该如何使用WhenAll辅助方法来避免这种情况。WhenAll辅助方法将会汇总一系列的任务对象,一旦其中某个出错,则会抛出“其中一个”异常。那么究竟是哪个异常?如果我们要处理所有的异常怎么办?我们这次就来详细讨论aw
阅读全文