从事件调用时,对话框挂起 [英] Dialog hangs when called from event
问题描述
使用Motorola提供的CoreScanner驱动程序,我正在尝试编写一个使用条形码扫描器的小型Winforms应用程序。我能够与扫描仪进行互动,并正确地为OnBarcodeEvent()注册一个回叫。在这个回调中,我有一个Dialog打开并显示用户需要填写的屏幕。在打开对话框后(使用Show()),程序挂起。如果我使用ShowDialog(),对话框工作,但对话框阻止OnBarcodeEvent事件。
Using the CoreScanner Driver provided by Motorola, I'm trying to write a small winforms application that uses a barcode scanner. I'm able to interact with the scanner just fine, and properly register a call back for the OnBarcodeEvent(). In this callback, I have a Dialog that opens and displays a screen that the user needs to fill in. Shortly after the dialog is opened (using Show()), the program hangs. If I use ShowDialog(), the dialog works, but the dialog is blocking the OnBarcodeEvent event.
我猜测发生了什么,是因为对话框被创建在事件线程上,使用Show()时出现竞争条件。由于Show()是非阻塞的,线程在显示对话框后会继续,然后消失。同时我的对话只是失去了它的父母和锁定?像我说的...我最好的猜测。
My guess to what is happening, is that since the dialog is getting created on the event thread, there is a race condition occurring when using Show(). Since Show() is non-blocking, the thread continues on after displaying the dialog and then dies out. Meanwhile my dialog just lost it's parent and locks up? Like i said... my best guess.
我如何补救这种情况?也就是说,如何编写我的对话框,以便在一个线程中创建不挂起?
How can I remedy the situation? That is, How do I write my dialog so that it can be created within a thread not hang?
推荐答案
我有一些经验与摩托罗拉/符号手持设备(MC9090),我猜SDK将是类似的。
I have some experience with Motorola/Symbol Handheld-Devices (MC9090) and I guess the SDK will be similar.
很难说没有看到代码,但我的猜测:
It's hard to say without seeing the code, but my guesses:
- 您的常见的非UI线程问题 - 所以请确保您使用UI线程打开对话框
- 本机摩托罗拉驱动程序崩溃 - 没有开玩笑,这对我来说很多 - 在我的情况下(WinCE设备上的紧凑框架),这不会冻结程序,但扫描仪在重新启动设备之前不会工作/接收任何消息
为了解决这个问题,您应该将用户对话框的显示/处理与事件分开,并确保在UI上调用-thread(Control.InvokeRequired / Control.Invoke)。
In order to fix this you should seperate the showing/handling of user-dialog away from the event and make sure you call this on the UI-thread (Control.InvokeRequired / Control.Invoke).
这篇关于从事件调用时,对话框挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!