在特定线程上调用一个代理C# [英] Invoke a delegate on a specific thread C#

查看:137
本文介绍了在特定线程上调用一个代理C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



说我有:



有没有办法让代理人在特定的线程上运行? pre> CustomDelegate del = someObject.someFunction;
Thread dedicatedThread = ThreadList [x];

我可以有一个一贯的背景长时间运行的线程,并在需要时调用我自己的代理。每次都必须使用相同的线程。





在一个专用的线程上,时间是我打算运行代理,并在 y 毫秒后挂起线程,并在我运行另一个代理时恢复线程。我看到这是不可能的。我将有一个委托队列,让线程的主要功能从它读取并运行。



为了澄清一个具体的例子,我有一个玩家线程的游戏系统。我希望每个玩家线程都可以在其上运行游戏活动的事件处理程序。如果事件处理程序花费太多时间,我希望能够暂停该特定播放器,直到下一个事件通过挂起它的线程。



因此,有一个专用的线程可以运行多个事件处理程序,我可以暂停一个特定的玩家的AI,以防万一发生错误或花费太长时间。

解决方案

我认为最好的解决方案是使用任务一个href =http://blogs.msdn.com/b/pfxteam/archive/2010/04/07/9990421.aspx =nofollow noreferrer> StaThreadScheduler 运行一个线程。



或者,您可以使用 ActionThread =nofollow noreferrer> Nito.Async 创建一个内置队列 Action 代理的正常线程。



然而,这些都不会直接解决另一个需求:能够暂停一个动作并继续使用另一个动作。要做到这一点,你需要在每一个动作中洒上同步点,并有一种方法来保存状态,重新排队,然后继续执行下一步操作。



所有这些复杂性几乎接近线程调度系统,所以我建议您退后一步,进行更多的重新设计。您可以允许每个操作排队到 ThreadPool (我建议只要每个操作都是一个任务对象)。您仍然需要洒上同步点,而不是保存状态并重新排队,您只需要暂停(阻止)它们。


Is there any way to get a delegate to run on a specific thread?

Say I have:

CustomDelegate del = someObject.someFunction;
Thread dedicatedThread = ThreadList[x];

Can I have a consistent background long running thread and invoke my own delegates on it whenever I need it. It has to be the same thread each time.

[Edit]

The reason why I want it to be on a dedicated thread is time is that I inten to run the delegate on it and suspend the thread after y milliseconds, and resume the thread when I run another delegate on it. I see this isn't possible. I will have a delegate queue and let the main function of the thread read and run from it.

To clarify with a concrete example, I have a game system with a bunch of player threads. I would like each playerthread to run event handlers for game events on it. If the event handlers take too much time I would like to be able to pause that particular player until the next event by suspending its thread.

Thus having a dedicated thread on which I can run multiple event handlers I can pause a particular player's AI in case it became bugged or is taking too long.

解决方案

I think the best solution is to use Task objects and queue them to an StaThreadScheduler running a single thread.

Alternatively, you could use the ActionThread in Nito.Async to create a normal thread with a built-in queue of Action delegates.

However, neither of these will directly address another need: the ability to "pause" one action and continue with another one. To do this, you'd need to sprinkle "synchronization points" throughout every action and have a way to save its state, re-queue it, and continue with the next action.

All that complexity is very nearly approaching a thread scheduling system, so I recommend taking a step back and doing more of a re-design. You could allow each action to be queued to the ThreadPool (I recommend just having each one be a Task object). You'll still need to sprinkle "synchronization points", but instead of saving state and re-queueing them, you'll just need to pause (block) them.

这篇关于在特定线程上调用一个代理C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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