.NET 创建新的调度程序

发布于 2024-11-23 15:19:07 字数 618 浏览 1 评论 0原文

我正在尝试使用调度程序创建第二个线程,以便我可以让主调度程序(用于 UI)完全无压力,并且让 UI 不断响应。

现在,我可以为每个子线程(或 C# 中的 void)创建多个线程,但我是否有可能创建一个新线程并获取它的调度程序,然后调用它?这就是我所做的:

Private CheckLoopThread As New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf CheckLoop))

CheckLoopThread.Priority = System.Threading.ThreadPriority.Lowest
CheckLoopThread.Start()
Dim Test As Windows.Threading.Dispatcher = Windows.Threading.Dispatcher.FromThread(CheckLoopThread)

但是,变量“Test”在执行“Nothing”之后。这怎么可能?是创建第二个调度程序的另一种方法吗?

欢迎以任何 .NET 形式提供答案。 Visual Basic 或 C#。我正在 .NET 4.0 框架上使用 VB.NET WPF。

提前致谢。

I am trying to create a second thread with dispatcher so that I can have the primary dispatcher (for the UI) completely stress-free, and have the UI constantly respondant.

Now, I could create multiple threads for each sub (or void in C#), but isn't it possible for me to create one new thread and grab it's dispatcher, and invoke to that? This is what I've done:

Private CheckLoopThread As New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf CheckLoop))

CheckLoopThread.Priority = System.Threading.ThreadPriority.Lowest
CheckLoopThread.Start()
Dim Test As Windows.Threading.Dispatcher = Windows.Threading.Dispatcher.FromThread(CheckLoopThread)

However, the variable "Test" is after execution "Nothing". How is this possible? Is the another way to create a second dispatcher?

Answers are appreciated in any .NET form. Visual Basic or C#. I am working in VB.NET WPF on the .NET 4.0 framework.

Thanks in advance.

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

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

发布评论

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

评论(3

猫烠⑼条掵仅有一顆心 2024-11-30 15:19:08

为什么要锁?

我更喜欢:

Dispatcher myDispatcher = null;

// create a manual reset event for crossthread signalling.
var dispatcherReadyEvent = new ManualResetEvent(false);

// create a new thread.
new Thread(new ThreadStart(() =>
{
    // get the current dispatcher (if it didn't exists
    // it will be created.
    myDispatcher = Dispatcher.CurrentDispatcher;
    // set the signal that the dispatcher is created.
    dispatcherReadyEvent.Set();
    // run the dispatcher.
    Dispatcher.Run();
})).Start();

// wait until the dispatcher is created on the thread.
dispatcherReadyEvent.WaitOne();

// run something on the dispatcher thread.
myDispatcher.Invoke(...);

Why locking?

I prefer:

Dispatcher myDispatcher = null;

// create a manual reset event for crossthread signalling.
var dispatcherReadyEvent = new ManualResetEvent(false);

// create a new thread.
new Thread(new ThreadStart(() =>
{
    // get the current dispatcher (if it didn't exists
    // it will be created.
    myDispatcher = Dispatcher.CurrentDispatcher;
    // set the signal that the dispatcher is created.
    dispatcherReadyEvent.Set();
    // run the dispatcher.
    Dispatcher.Run();
})).Start();

// wait until the dispatcher is created on the thread.
dispatcherReadyEvent.WaitOne();

// run something on the dispatcher thread.
myDispatcher.Invoke(...);
剧终人散尽 2024-11-30 15:19:08

Dispatcher.FromThread(... ) 不会创建 Dispatcher,如果尚未为线程创建 Dispatcher,则返回 null。要为线程创建调度程序,您必须访问 Dispatcher.CurrentDispatcher 至少在您的 CheckLoopThread 上运行一次。正如 MSDN 上针对 Dispatcher.CurrentDispatcher 所说:

如果 Dispatcher 没有与当前线程关联,则创建一个新的 Dispatcher
将创建调度程序。 FromThread 的情况并非如此
方法。如果没有调度程序,FromThread 将返回 null
与指定线程关联

Dispatcher.FromThread(...) will not create a Dispatcher and will return null if a Dispatcher has not already been created for the thread. To create a Dispatcher for a thread, you will have to access Dispatcher.CurrentDispatcher at least once on your CheckLoopThread. As it says on MSDN for Dispatcher.CurrentDispatcher:

If a Dispatcher is not associated with the current thread, a new
Dispatcher will be created. This is not the case with the FromThread
method. FromThread will return null if there is not a dispatcher
associated with the specified thread

咽泪装欢 2024-11-30 15:19:08

我实际上正在创建很多这样的调度程序,我想正确的方法是以下几行:

object theLock = new object();
Dispatcher dispatcher = null;

lock (theLock)
{
    new Thread(new ThreadStart(() =>
    {
        lock (theLock)
        {
            dispatcher = Dispatcher.CurrentDispatcher;
            Monitor.Pulse(theLock);
        }
        Dispatcher.Run();
    })).Start();

    Monitor.Wait(theLock);
}

dispatcher.Invoke(...);

所有锁定似乎都很复杂,但理论上 Start() 方法可以在 之前返回>dispatcher 实际上已设置,因此调用它可能会导致没有锁的 NullReferenceException

I'm actually creating a lot of these dispatchers, I guess the proper way is something along the following lines:

object theLock = new object();
Dispatcher dispatcher = null;

lock (theLock)
{
    new Thread(new ThreadStart(() =>
    {
        lock (theLock)
        {
            dispatcher = Dispatcher.CurrentDispatcher;
            Monitor.Pulse(theLock);
        }
        Dispatcher.Run();
    })).Start();

    Monitor.Wait(theLock);
}

dispatcher.Invoke(...);

It seems complicated with all the locking, but theoretically the Start() method can return before dispatcher is actually set, so a call to to it might result in a NullReferenceException without the locks.

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