C#控制台应用程序中的调度程序存在问题 [英] Problems with dispatcher in C# console application

查看:111
本文介绍了C#控制台应用程序中的调度程序存在问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在正确的工作线程上调用在计时器线程上运行的方法. Invoke/BeginInvoke程序正在为我工​​作.有2个线程共享一个线程间数据容器以进行数据交换.一种是填充队列,一种是处理队列.如果队列在空状态后被填充,则会引发事件.所有问题均由计时器引起,该计时器在其已发生的事件上打开新线程.我正在使用分派器在正确的线程上分派,但除此分派器外,其他所有方法都可以正常工作. :-)

I need to invoke method runed on timer thread on right working thread. Invoke/BeginInvoke process is working for me. There are 2 threads which shares one inter thread data container for data exchange. One is filling queue, one shuld process the queue. Queue raises event if it is filled after empty state. All the problem is caused by timer, which open new thread on its elapsed event. I am using Dispatcher to dispatch on the right thread, but everything works, except this Dispatcher. :-)

请,有人看到问题出在哪里吗?

Please, does anybody see where the problem is?

完整的测试代码在这里: http://pastebin.com/jqYbR9PS .

Complete test code is here: http://pastebin.com/jqYbR9PS.

调试输出是这样的:

App Thread ID: 9
Processor Thread ID: 10
Processor Dispatcher Thread ID: 10
The thread '<No Name>' (0x888) has exited with code 0 (0x0).
Processor QueueListener caller Thread ID: 12
Processor Dispatcher Thread ID: 10
Processor invoking ProcessQueue.
...here shut be processing output...
Processor invoked ProcessQueue.
App Thread ID on end: 9
The thread 'vshost.RunParkingWindow' (0x17c4) has exited with code 0 (0x0).
The thread '<No Name>' (0x820) has exited with code 0 (0x0).
The program '[5760] TestingConsoleApplication.vshost.exe: Managed (v4.0.30319)' has exited with code 0 (0x0).

从未调用/调用ProcessQueue.

ProcessQueue is never called/invoked.

谢谢.

推荐答案

您实际上需要通过调用

You need to actually start the dispatcher by calling Dispatcher.Run() for the dispatcher to process the calls invoked to it.

从控制台应用程序中使用Dispatcher是很奇怪的-Dispatcher用于WPF应用程序,但我想它可以工作.

It's pretty strange to use a Dispatcher from a console application - the Dispatcher is used for WPF applications but I guess it will work.

请注意,对Dispatcher.Run()的调用不会返回-它将进入循环,直到您调用

Note that the call to Dispatcher.Run() won't return - it will enter a loop until you call Dispatcher.BeginInvokeShutdown()

请参见 查看全文

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