【发布时间】:2015-08-26 07:05:47
【问题描述】:
在 .NET (C#) 应用程序中,我希望有一个调度程序系统,类似于 iOS 上的 dispatch_async()。
我在多个 stackoverflow 线程上读到 Dispatcher 只能在 WPF 应用程序的上下文中使用,但我不明白为什么。
我通过阅读 MSDN 文档编写了以下代码,并且它正在运行。 我有什么理由停止使用这种机制吗?
我不想在这个过程的后期意识到我因为一些我不知道的事情而被卡住了。 (我是 C# 和 .NET 的新手)。
using System;
using System.Threading;
using System.Windows.Threading;
namespace ExcelLeak
{
class Program
{
public static Dispatcher mainSerialQueue;
public static Dispatcher backgroundSerialQeueue;
static void Main(string[] args)
{
mainSerialQueue = Dispatcher.CurrentDispatcher;
Thread serialQueueThread = new Thread(() =>
{
backgroundSerialQeueue = Dispatcher.CurrentDispatcher;
mainSerialQueue.BeginInvoke((System.Action)(() =>
{
doSomeWork();
}));
Dispatcher.Run();
});
serialQueueThread.Start();
Dispatcher.Run();
}
}
public static void doSomeWork() {
mainSerialQueue.BeingInvoke(...)
}
}
我在这里所做的基本上是获取主线程的调度程序,我称之为mainSerialQueue,然后创建另一个线程,并为其获取调度程序,我称之为backgroundSerialQeueue。
【问题讨论】:
标签: c# .net command-line-interface dispatcher