简体   繁体   English

在Windows Mobile应用程序中使用UnhandledException的问题

[英]Problem using UnhandledException in Windows Mobile app

I have a Windows Mobile program that accesses an attached device through a third-party DLL. 我有一个Windows Mobile程序,该程序通过第三方DLL访问连接的设备。 Each call to the device can take an unknown length of time, so each call includes a timeout property. 对设备的每次调用都可能花费未知的时间长度,因此每个调用都包含超时属性。 If the call takes longer than the specified timeout to return, the DLL instead throws an exception which my app catches with no problem. 如果调用花费的时间比指定的超时时间长,则DLL会引发一个异常,我的应用程序可以毫无问题地捕获该异常。

The problem that I have is with closing the application. 我的问题是关闭应用程序。 If my application has made a call to the DLL and is waiting for the timeout to occur, and I then close the application before the timeout occurs, my application locks up and requires the PDA to be rebooted. 如果我的应用程序已调用DLL并等待超时发生,然后在超时发生前关闭了该应用程序,则我的应用程序将锁定并要求重新启动PDA。

I can ensure that the application waits for the timeout before closing, under normal conditions. 在正常情况下,我可以确保应用程序在关闭之前等待超时。 However, I am trying to use AppDomain.CurrentDomain.UnhandledException to catch any unhandled exceptions in the program and use the event to wait for this pending timeout to occur so the program can be closed finally. 但是,我试图使用AppDomain.CurrentDomain.UnhandledException捕获程序中任何未处理的异常,并使用事件等待此挂起的超时发生,以便最终关闭程序。

My problem is that this event doesn't seem to stick around long enough. 我的问题是此事件似乎持续的时间不够长。 If I put a MessageBox.Show("unhandled exception"); 如果我放入MessageBox.Show("unhandled exception"); line in the event, and then throw a new unhandled exception from my application's main form, I see the message box for a split second but then it disappears without my having clicked the OK button. 行,然后从应用程序的主窗体中引发新的未处理的异常,我看到消息框有一秒钟,但随后消失了,而无需单击“确定”按钮。

The documentation I've found on this event suggests that by the time it's called the application is fully committed to closing and the closing can't be stopped, but I didn't think it meant that the event method itself won't finish. 我在此事件中发现的文档表明,到调用它的时候,应用程序已完全致力于关闭,并且无法停止关闭,但我不认为这意味着事件方法本身不会完成。 What gives (I guess that's the question)? 产生了什么(我想这就是问题)?

Update: In full windows (Vista) this works as expected, but only if I use the Application.ThreadException event, which doesn't exist in .Net CF 2.0. 更新:在完整窗口(Vista)中,此功能按预期工作,但仅当我使用Application.ThreadException事件时,该事件在.Net CF 2.0中不存在。

I came across this problem as well. 我也遇到了这个问题。 This is a known issue in .NET CF (v2.0), but I also had it while using v3.5 (although the situations in which it occurs are more specific). 这是.NET CF(v2.0)中的一个已知问题,但是我在使用v3.5时也遇到了此问题(尽管发生这种情况的情况更加具体)。 You can find the (old and still active) bug report here . 您可以在此处找到(旧的且仍处于活动状态)错误报告。

Calling MessageBox.Show() causes it to close immediately, but in my case there were two workarounds: 1) Call the MessageBox.Show() a second time. 调用MessageBox.Show()使它立即关闭,但是在我的情况下,有两种解决方法:1)第二次调用MessageBox.Show()。 It then does block until closed by the user. 然后它会阻塞直到被用户关闭。 You can check the first MessageBox.Show() closed prematurely by checking the DialogResult. 您可以通过检查DialogResult来提前关闭第一个MessageBox.Show()。 I don't remember which result it returned exactly when it failed, I remember it giving a non-default result. 我不记得它在失败时准确返回了哪个结果,我记得它给出了非默认结果。

2) Create a custom Form and call ShowDialog() on that. 2)创建一个自定义窗体,并在其上调用ShowDialog()。 It worked for me, but others have reported it doesn't work. 它对我有用,但是其他人报告它不起作用。 You could also call Show() and make it blocking yourself (don't forget to call Application.DoEvents() so it keeps processing events). 您还可以调用Show()并使其阻塞自己(别忘了调用Application.DoEvents()以便它继续处理事件)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM