更换任务调度程序在C#中使用一个定制了一辆 [英] Replacing the task scheduler in C# with a custom-built one

查看:545
本文介绍了更换任务调度程序在C#中使用一个定制了一辆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在想,如果我可以改变任务调度程序映射使用C#任务在.NET中真正的操作系统线程,或者如果我需要重新编译,比方说,在Mono运行时做到这一点。谢谢你。

I was wondering if I can change the task scheduler that maps tasks to the real OS threads in .NET using C#, or if I need to recompile, say, the Mono runtime to do this. Thanks.

推荐答案

如果你指的 System.Threading.Tasks 那么,你需要的是子类的TaskScheduler 然后你可以使用你的类的对象初始化 TaskFactory 。有一个例如的MSDN研究。我还发现了一个的例如,在博客PSY codeDeveloper

System.Threading.Tasks

If you refer to System.Threading.Tasks then what you need is to subclass TaskScheduler and then you can use an object of your class to initialize a TaskFactory. There is an example in MSDN. I have also found an example in the blog psyCodeDeveloper.

除此之外,您可以使用的SynchronizationContext 以处理方法发布到线程池(与线程池的任务。 QueueUserWorkItem 为例)得到处理。

Aside from that you could use SynchronizationContext to handle the way the tasks posted to the ThreadPool (with ThreadPool.QueueUserWorkItem for example) get handled.

有关,你可能会感兴趣的一系列理解的SynchronizationContext在$ C $的CProject(的第1部分部分2 部分3 )。

For that you may be interested in the series Understanding SynchronizationContext at CodeProject (part 1, part 2 and part 3).

至于在无扩展的自定义调度,您还可以使用的SynchronizationContext上面提到的,有关详细信息,查看教程在 introtorx.com 特别第4部分:并发

As for the custom schedulers in Reactive Extensions, you can also use the SynchronizationContext mentioned above, for more information check the tutorial at introtorx.com in particular Part 4: Concurrency.

当然,你可以滚自己线程池,但这样做是不明智的。除此之外,你可以手动处理您的线程 - 老办法

Of course you can roll your own thread pool, although doing that is not advised. Aside from that you could handle your threads manually - the old way.

其他的方法来处理任务,包括调度计时器,并有专门的线程做的工作。

Other approaches to handle tasks include scheduling with timers and having dedicated threads to do the work.

<打击>作为 Theraot图书馆你会找到类<一部分href="https://github.com/theraot/Theraot/blob/14f808e4e89d7843349f5394a986bbee9a8e73f4/Core/Theraot/Threading/Work.cs"相对=nofollow>工作的是基于一个锁定空闲队列和可以被配置为具有任何数目的专用线程,也等待任务的线程的贡献他们的时间来执行的任务,任何额外的工作委托给线程池。这是一个不断努力的backport System.Threading.Tasks到.NET 2.0的一部分。

As part of the Theraot Libraries you will find the class Work which is based on a lock free queue and can be configured to have any number of dedicated threads, also threads waiting on tasks contribute their time to execute tasks, any extra work is delegated to the ThreadPool. This is part of an ongoing effort to backport System.Threading.Tasks to .NET 2.0.

Theraot图书馆工作类离开我们已经有一段时间了,的部分回港 System.Threading.Tasks 的.NET 2.0可与自定义的TaskScheduler支持。

In Theraot Libraries the Work class has been gone for a while now, a partial back port of System.Threading.Tasks for .NET 2.0 is available with support for custom TaskScheduler.

全disclousre:由于缺乏想象力的名字暗示,我是Threaot库的作者。对不起了丢失的文件,我愿意帮助在使用图书馆的任何方面。请报告任何错误,我目前(2013年6月26日)没有已知的主分支的bug。

Full disclousre: As the unimaginative name suggest, I'm the author of the Threaot libraries. Sorry by the missing documentation, I willing to help in any aspect of using the libraries. Please report any bugs, I have currently (2013-06-26) no known bugs in the master branch.

这篇关于更换任务调度程序在C#中使用一个定制了一辆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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