我以为等待在与调用方相同的线程上继续进行,但似乎并没有 [英] I thought await continued on the same thread as the caller, but it seems not to

查看:51
本文介绍了我以为等待在与调用方相同的线程上继续进行,但似乎并没有的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为关于async/await的要点之一是,当任务完成时,继续在调用await的同一上下文中运行,在我的情况下,这将是UI线程.

例如:

Debug.WriteLine("2: Thread ID: " + Thread.CurrentThread.ManagedThreadId);
await fs.ReadAsync(data, 0, (int)fs.Length);
Debug.WriteLine("3: Thread ID: " + Thread.CurrentThread.ManagedThreadId);

我不希望这样:

2: Thread ID: 10
3: Thread ID: 11

有什么作用?为什么延续的线程ID与UI线程不同?

根据本文 [^],我需要显式调用ConfigureAwait来更改上下文的行为!

解决方案

await时,默认情况下,await运算符将捕获当前的上下文",并将其用于恢复async方法. >

此上下文"为SynchronizationContext.Current,除非它为null,在这种情况下为TaskScheduler.Current. (如果没有当前正在运行的任务,则TaskScheduler.Current与线程池任务计划程序TaskScheduler.Default相同).

请务必注意,SynchronizationContextTaskScheduler不一定表示特定的 thread . UI SynchronizationContext将安排工作到UI线程;但是ASP.NET SynchronizationContext不会将工作安排在特定线程上.

我怀疑导致问题的原因是您太早调用了async代码.当应用程序启动时,它只有一个普通的旧常规线程.该线程仅在执行Application.Run之类的操作时才成为UI线程.

I thought one of the points about async/await is that when the task completes, the continuation is run on the same context when the await was called, which would, in my case, be the UI thread.

So for example:

Debug.WriteLine("2: Thread ID: " + Thread.CurrentThread.ManagedThreadId);
await fs.ReadAsync(data, 0, (int)fs.Length);
Debug.WriteLine("3: Thread ID: " + Thread.CurrentThread.ManagedThreadId);

I would NOT expect this:

2: Thread ID: 10
3: Thread ID: 11

What gives? Why is the thread ID for the continuation different than the UI thread?

According to this article[^] I would need to explicitly call ConfigureAwait to change the behavior of the continuation context!

解决方案

When you await, by default the await operator will capture the current "context" and use that to resume the async method.

This "context" is SynchronizationContext.Current unless it is null, in which case it is TaskScheduler.Current. (If there is no currently-running task, then TaskScheduler.Current is the same as TaskScheduler.Default, the thread pool task scheduler).

It's important to note that a SynchronizationContext or TaskScheduler does not necessarily imply a particular thread. A UI SynchronizationContext will schedule work to the UI thread; but the ASP.NET SynchronizationContext will not schedule work to a particular thread.

I suspect that the cause of your problem is that you are invoking the async code too early. When an application starts, it just has a plain old regular thread. That thread only becomes the UI thread when it does something like Application.Run.

这篇关于我以为等待在与调用方相同的线程上继续进行,但似乎并没有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆