gpt4 book ai didi

c# - 创建新的调度程序

转载 作者:太空宇宙 更新时间:2023-11-03 13:02:20 25 4
gpt4 key购买 nike

在我的 Windows Phone 8.1 应用程序中,我需要为 Thread 创建一个新的 Dispatcher。通常,我们可以这样做:

Dispatcher dispatcher = null;

ManualResetEvent dispatcherReadyEvent = new ManualResetEvent(false);

new Thread(new ThreadStart(() =>
{
dispatcher = Dispatcher.CurrentDispatcher;
dispatcherReadyEvent.Set();
Dispatcher.Run();
})).Start();

dispatcherReadyEvent.WaitOne();

dispatcher.Invoke(...);

问题是使用 Windows Phone 8.1,我无法访问 Thread 类。我不明白如何创建线程并从中提取分派(dispatch)器。提前谢谢你。

最佳答案

At a certain point I wanna stop parallelism and I want to make the calls sequential (following their order). I'd like to create a sort of channel. To do so I need a thread called, maybe, ChannelThread. If I had the dispatcher of that thread I can just call the functions on that dispatcher. So, I'll be sure the calls happen sequentially.

首先,停止从线程的角度思考。在软件开发的这一点上,线程是一个实现细节。我建议采用可用的最高级别解决方案。

立即想到的两个是响应式扩展 (Rx) 和 TPL 数据流。由于您的问题中没有太多上下文,因此很难说 Rx 与您的解决方案集成的难易程度。 TPL 数据流提供了一个简单的 FIFO 缓冲区,您可以像这样使用它:

var fifo = new ActionBlock<Action>(action => action());

默认情况下,数据流 block 在线程池上执行并将它们的并发限制为一次一个,所以这就是您所需要的。

将工作排队到 block :

fifo.Post(() => { /* work */ });

关于c# - 创建新的调度程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31970334/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com