简体   繁体   English

Windows Mobile应用程序避免未处理的异常处理

[英]Windows Mobile Application avoiding Unhandled Exception Handling

We have error handling around our Windows Mobile application similar to the following: 我们围绕Windows Mobile应用程序进行的错误处理类似于以下内容:

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [MTAThread]
    static void Main()
    {
        try
        {
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(OnUnhandledException);
            LogWriter.WriteDebugLog("******* Application Start *******");
            Application.Run(new Form1());
        }
        catch (Exception ex)
        {
            LogWriter.WriteDebugLog("LOG MAIN: " + ex.Message + "\r\nTrace: " + ex.StackTrace);
            MessageBox.Show("An unexpected error has occured, please try again. If the problem continues please contact application provider.", "Fatal Error");
        }
        finally
        {
            AppDomain.CurrentDomain.UnhandledException -= new UnhandledExceptionEventHandler(OnUnhandledException);
            try
            {
                //System.Diagnostics.Process proc = System.Diagnostics.Process.GetCurrentProcess();
                //proc.Kill();
            }
            catch { }
            LogWriter.WriteDebugLog("******* Application Exit *******");
        }
    }

    public static void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        string application_name = sender.ToString();
        Exception except = (Exception)e.ExceptionObject;
        string errormessage = "Application " + application_name + " [ Exception " + except.Message + " ]" + Environment.NewLine + except.StackTrace;

        //MessageBox.Show(errormessage, "Fatal Error");
        LogWriter.WriteDebugLog(errormessage);
        AppDomain.CurrentDomain.UnhandledException -= new UnhandledExceptionEventHandler(OnUnhandledException);
    }
}

Despite this error handling methodology, we have received logs from users where the application has simply terminated without recording an exit or an unhandled exception or a LOG MAIN exception. 尽管采用了这种错误处理方法,我们仍从用户那里接收了日志,在这些日志中,应用程序只是终止而没有记录退出或未处理的异常或LOG MAIN异常。

I have been trying to reproduce this by creating a very simple application and throwing around various types of exception strategy to kill the application, but with no success. 我一直在尝试通过创建一个非常简单的应用程序并抛出各种类型的异常策略来杀死该应用程序来重现此问题,但是没有成功。 Every time I throw any kind of exception it seems to be caught by the code above. 每当我抛出任何形式的异常时,上面的代码似乎都将其捕获。

If anyone has any ideas as to what kind of issues might skip this error handling it would be greatly appreciated as this might move our investigations forwards. 如果有人对什么样的问题可能跳过此错误处理有任何想法,将不胜感激,因为这可能会使我们的调查向前推进。

Kind regards, Graham. 亲切的问候,格雷厄姆。


Edit: Slightly more information in case it is relevant as requested. 编辑:如果需要,可以提供更多信息。

The applications in question are a large variety of Windows Mobile 6.5 applications, mostly running on Motorola MC65 devices although some are on Psion EP10 devices and some are on other devices too (so it is unlikely to be device-specific). 有问题的应用程序是各种各样的Windows Mobile 6.5应用程序,大多数运行在Motorola MC65设备上,尽管有些在Psion EP10设备上,有些也在其他设备上(因此不太可能是特定于设备的)。

The applications all use SQLCe and Merge Replication. 所有应用程序都使用SQLCe和合并复制。 Many of them use third-party controls for displaying on forms. 他们中的许多人使用第三方控件在表单上显示。 All of them use a certain amount of PInvoking for varying purposes. 它们都使用一定数量的PInvoking用于不同目的。

The crashes themselves do not appear to be happening at any particular point in the application and not very often even then but when we review the logs we see the application logging its progress (including memory usage and various other useful details) and then it simply terminates without any record. 崩溃本身似乎并不是在应用程序中的任何特定点发生的,即使在那时也很少发生,但是当我们查看日志时,我们看到应用程序记录了它的进度(包括内存使用情况和各种其他有用的详细信息),然后它终止了没有任何记录。

"Despite this error handling methodology, we have received logs from users where the application has simply terminated without recording an exit or an unhandled exception or a LOG MAIN exception." “尽管采用了这种错误处理方法,但我们已经从用户那里收到了日志,在这些日志中,应用程序只是终止了,而没有记录退出或未处理的异常或LOG MAIN异常。”

There are exceptions that are not handled by any .NET excption handler: for example native code referenced by your app function useage may crash. 某些.NET例外处理程序无法处理的例外情况:例如,您的应用函数使用情况引用的本机代码可能会崩溃。

Possibly you have a more simple problem: if the device resources geting low it sends a hibernate message ( http://msdn.microsoft.com/en-us/library/aa925791.aspx ) to all windows. 可能您有一个更简单的问题:如果设备资源不足,它将向所有窗口发送休眠消息( http://msdn.microsoft.com/zh-cn/library/aa925791.aspx )。 If a process does not release resources and respond to this request, it may be just killed by the OS, if the resources remain low after the broadcast. 如果进程没有释放资源并响应此请求,则在广播后资源仍然不足的情况下,它可能会被OS杀死。

I already had some support calls about this (I am doing WM/PPC support for 9 years now) and they were fixed by lowering the resource usage on the devices and by implementing a WM_HIBERNATE handler. 我已经收到了一些与此有关的支持电话(我已经有9年的WM / PPC支持),并且通过降低设备上的资源使用率并实现WM_HIBERNATE处理程序来解决这些问题。

You also need to catch unhandled Thread exceptions in a similar manner: 您还需要以类似的方式捕获未处理的线程异常:

static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);


    Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
    AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

    Application.Run(new Form1());
}

Ref.: Application.ThreadException 参考: Application.ThreadException

@Mitch Wheat我相信我们在这里谈论的是使用.NET CF 3.5的Windows Mobile6。.NETCF不支持ThreadException,所以您不能这样做。

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

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