wpf 我如何退出(停止)我的 Dispatcher?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24821131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I exit (stop) my Dispatcher?
提问by Norick
I would like to stop my dispatcher which has been started in the main(), when the user clicks a button.
当用户单击按钮时,我想停止在 main() 中启动的调度程序。
private void button_start_Click(object sender, RoutedEventArgs e)
{
...
Camera.EventFrame -= onFrameEventFocusCam; //unsubscribe event
while (!Dispatcher.HasShutdownStarted)
{
// test if Dispatcher started shutdown --> ok, it does but never finishs...!
}
...
}
private void onFrameEventFocusCam(object sender, EventArgs e)
{
...
Dispatcher.Invoke(new Action(() =>
{
// Convert bitmap to WPF-Image
var bmp = new Bitmap(bitmap);
var hBitmap = bmp.GetHbitmap();
System.Windows.Media.ImageSource wpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
image_fokus.Source = wpfBitmap;
image_fokus.Stretch = System.Windows.Media.Stretch.UniformToFill;
DeleteObject(hBitmap);
bitmap.Dispose();
}));
GC.Collect();
}
When I run this code I get following error message while it is stopped in the onFrameEventFocusCam:
当我运行此代码时,它在以下位置停止时收到以下错误消息onFrameEventFocusCam:
Error-Message: Thread was interrupted from a waiting state.
回答by Thomas Levesque
When you call BeginInvokeDispatcher, it causes the dispatcher to shut down asynchronously; it still needs to finish processing the current dispatcher frame before it shuts down. But since you're waiting in a loop for the dispatcher to shut down, the current frame never completes, so the dispatcher can't complete the shutdown. Instead, you can subscribe to the ShutdownFinishedevent to detect when the dispatcher shuts down.
当您调用时BeginInvokeDispatcher,它会导致调度程序异步关闭;它仍然需要在关闭之前完成对当前调度程序帧的处理。但是由于您在循环中等待调度程序关闭,因此当前帧永远不会完成,因此调度程序无法完成关闭。相反,您可以订阅ShutdownFinished事件以检测调度程序何时关闭。
As for the error you're getting in onFrameEventFocusCam, I think this is because the dispatcher is shutting down, so it can't process the Invoke; from the documentation:
至于您遇到的错误onFrameEventFocusCam,我认为这是因为调度程序正在关闭,因此无法处理Invoke;从文档:
Once the shutdown process begins, all pending work items in the queue are aborted.
一旦关闭过程开始,队列中所有挂起的工作项都会中止。
I'm not sure what you're trying to do, but shutting down the main dispatcher is very probably notthe way to do it, as it will effectively stop the app...
我不确定你要做什么,但关闭主调度程序很可能不是这样做的方法,因为它会有效地停止应用程序......
回答by Alon Catz
It's not clear what you original intention was.
不清楚你的初衷是什么。
This code should hang (almost) forever because HasShutdownStarted doesn't become true until the application exits.
此代码应该(几乎)永远挂起,因为 HasShutdownStarted 在应用程序退出之前不会变为真。
while (!Dispatcher.HasShutdownStarted)
{
// test if Dispatcher started shutdown --> ok, it does but never finishs...!
}
This code schedules execution of the delegate on the UI thread. Looks perfectly fine.
此代码计划在 UI 线程上执行委托。看起来很好。
private void onFrameEventFocusCam(object sender, EventArgs e)
{
...
Dispatcher.Invoke(new Action(() =>
{
...
}
}
This code looks potentially problematic because if DeleteObject actually releases the handle, than the handle may be released twice, once in Delete and once in Dispose. This can throw.
这段代码看起来可能有问题,因为如果 DeleteObject 真的释放了句柄,那么句柄可能会被释放两次,一次是在 Delete 中,一次是在 Dispose 中。这个可以扔。
DeleteObject(hBitmap);
bitmap.Dispose();
回答by mr100
To make stop dispatcher running on your main application thread you should call return from action passed to Dispatcher.Invoke.
要使停止调度程序在您的主应用程序线程上运行,您应该调用 return from action 传递给 Dispatcher.Invoke。

