.NET异步,可在单个线程任务之间的时间片? [英] .NET async, can a single thread time-slice between tasks?

查看:148
本文介绍了.NET异步,可在单个线程任务之间的时间片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让异步编程把握在C#/。NET。我读了布朗大学的网站上的一篇文章(链接) cs168当然,它定义异步编程为同一线程内交错的任务。它说,现在我们可以引入异步模型。在这个模型中,任务交错彼此,但在单个控制线,并显示在一个身影交错的很清楚。但我似乎无法得到两项任务.NET中的同一个线程中交错。有没有办法做到这一点?

I'm trying to get a grasp on asynchronous programming in C#/.NET. I read an article (link) on Brown University's website for the cs168 course that defines asynchronous programming as interleaving tasks within the same thread. It says, "Now we can introduce the asynchronous model... In this model, the tasks are interleaved with one another, but in a single thread of control", and shows interleaving very clearly in a figure. But I can't seem to get two tasks to interleave within the same thread in .NET. Is there a way to do that?

我写了一些简单的应用程序,试图验证这一理论,但我不知道如果我做正确。主程序输出到屏幕上,每隔一段时间,使用 Thread.sleep代码()模拟的工作。异步任务不一样。如果使用多个线程,输出被交织。但我想测试在单个线程。

I wrote some simple apps to try to test this theory, but I'm not sure if I'm doing it correctly. The main program outputs to the screen every so often, using Thread.Sleep() to simulate work. The asynchronous task does the same. If multiple threads are used, the output is interleaved. But I'm trying to test on a single thread.

我有一个运行在UI线程上一切WPF应用程序,但任务和主要程序总是依次输出。我创建并启动任务是这样的:

I have a WPF app that runs everything on the UI thread, but the task and main program always output sequentially. I create and start the task like this:

var taskFactory = new TaskFactory(TaskScheduler.FromCurrentSynchronizationContext());
var task = taskFactory.StartNew(workDelegate);

我有开始使用 Task.Run(workDelegate)任务代表一个控制台应用程序; ,但它们运行在不同的线程池中的线程。我不知道如何让他们在同一线程上运行两个。如果我尝试我在WPF中使用的相同的方法,我得到一个运行InvalidOperationException异常,目前的SynchronizationContext不得用作的TaskScheduler。

I have a console app that starts the task delegate using Task.Run(workDelegate);, but that runs them on different thread pool threads. I'm not sure how to make them both run on the same thread. If I try the same approach I used in WPF I get a runtime InvalidOperationException, "The current SynchronizationContext may not be used as a TaskScheduler".

推荐答案

多重任务不会自动在单个线程交错。要做到这一点,你必须指定线程被允许削减到另一个任务在任务code点。您可以通过像 <$ C的机制做到这一点$ C>等待Task.Yield 。如果你在一个线程中运行,该线程将不能够让其他的工作取得进展,除非它明确地产生。

Multiple tasks won't automatically be interleaved on a single thread. To do that, you have to specify the points in task code where the thread is allowed to cut over to another task. You can do this via a mechanism like await Task.Yield. If you're running on a single thread, the thread will not be able to allow other work to progress unless it explicitly yields.

当您使用的TaskScheduler 来开始每个任务,在WPF进度消息泵每个任务的UI线程上运行,它们将依次运行。

When you use your TaskScheduler to start every task, the message pump in WPF schedules each task to run on the UI thread, and they will run sequentially.

我有开始使用Task.Run(workDelegate)任务代表一个控制台应用程序;但是在运行它们放在不同的线程池中的线程。我不知道如何让他们在同一线程上运行两个

I have a console app that starts the task delegate using Task.Run(workDelegate);, but that runs them on different thread pool threads. I'm not sure how to make them both run on the same thread.

您将需要一个自定义的的SynchronizationContext 安装到这允许您发布工作,该线程的线程。

You would need to install a custom SynchronizationContext into a thread which allowed you to post work to that thread.

这篇关于.NET异步,可在单个线程任务之间的时间片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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