如何中止 CCR 线程\任务?

发布于 2024-07-27 13:24:24 字数 125 浏览 2 评论 0原文

我想在使用 CCR 的项目中执行任务时实现超时。 基本上,当我将一个项目发布到端口或将一个任务排队到 DispatcherQueue 时,如果花费的时间超过某个配置的时间,我希望能够中止该任务或其运行的线程。 我怎样才能做到这一点?

I want to implement a timeout on the execution of tasks in a project that uses the CCR. Basically when I post an item to a Port or enqueue a Task to a DispatcherQueue I want to be able to abort the task or the thread that its running on if it takes longer than some configured time. How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

霞映澄塘 2024-08-03 13:24:24

你能确认你所问的内容吗? 您是否在调度程序中运行长期任务? 终止线程会破坏 CCR 模型,因此您需要能够向线程发出信号以完成其工作并让出。 假设这是一个完成速度不够快的循环,您可以选择将计时器排入队列:

var resultTimeoutPort = new Port<DateTime>();
dispatcherQueue.EnqueueTimer(TimeSpan.FromSeconds(RESULT_TIMEOUT), 
                             resultTimeoutPort);

并确保阻塞线程具有对 resultTimeoutPort 的可用引用。 在阻塞循环中,退出条件之一可能是:

do
{
    //foomungus amount of work
}while(resultTimeoutPort.Test()==null&&
       someOtherCondition)

如果我找错了树,请发布更多信息。

Can you confirm what you are asking? Are you running a long-lived task in the Dispatcher? Killing the thread would break the CCR model, so you need to be able to signal to the thread to finish its work and yield. Assuming it's a loop that is not finishing quick enough, you might choose to enqueue a timer:

var resultTimeoutPort = new Port<DateTime>();
dispatcherQueue.EnqueueTimer(TimeSpan.FromSeconds(RESULT_TIMEOUT), 
                             resultTimeoutPort);

and ensure the blocking thread has available a reference to resultTimeoutPort. In the blocking loop, one of the exit conditions might be:

do
{
    //foomungus amount of work
}while(resultTimeoutPort.Test()==null&&
       someOtherCondition)

Please post more info if I'm barking up the wrong tree.

安人多梦 2024-08-03 13:24:24

您可以在 CCR“接收”处理程序的开头(或在通过委托调用您的方法的方法中)注册线程 (Thread.CurrentThread)。 然后,您可以进行定期检查,并在必要时中止,这与手动创建线程的方式基本相同。 问题是,如果您使用自己的 Microsoft.Ccr.Core.Dispatcher 和固定数量的线程,我认为一旦中止它们就没有办法恢复这些线程(根据我的测试)。 因此,如果您的调度程序有 5 个线程,则无论注册了哪些任务,您都只能中止 5 次,然后发布将不再起作用。 但是,如果您使用 CLR 线程池构造 DispatcherQueue,则您中止的任何 CCR 线程都将被自动替换,并且您不会遇到该问题。 据我所知,虽然建议使用 CCR 调度程序,但我认为在这种情况下使用 CLR 线程池是最佳选择。

You could register the thread (Thread.CurrentThread) at the beginning of your CCR "Receive" handler (or in a method that calls your method via a delegate). Then you can do your periodic check and abort if necessary basically the same way you would have done it if you created the thread manually. The catch is that if you use your own Microsoft.Ccr.Core.Dispatcher with a fixed number of threads, I don't think there is a way to get those threads back once you abort them (based on my testing). So, if your dispatcher has 5 threads, you'll only be able to abort 5 times before posting will no longer work regardless of what tasks have been registered. However, if you construct a DispatcherQueue using the CLR thread pool, any CCR threads you abort will be replaced automatically and you won't have that problem. From what I've seen, although the CCR dispatcher is recommended, I think using the CLR thread pool is the way to go in this situation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文