views:

197

answers:

2

Hello,

It seems I tend to attract strange issues. This time, I have written a C# application, and handled most of the exceptions I can find. The problem is, when I run the installed/bundled version on any PC for the first time in a day (after the PC has been shut down and started after a while) it comes across some error and has to shutdown the application (even though the try-catch block surrounding the Main() does not fire). The application does not throw the same error on subsequent runs.

static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            // your event handler
            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
            // your event handler
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            // you try catch block and Application.Run

            try
            {
#if(!DEBUG)
                    Application.EnableVisualStyles();
                    Application.Run(new newTerminal());
#else
                Application.EnableVisualStyles();
                newTerminal terminal = new newTerminal();
                terminal.ShowDialog();
#endif
            }
            catch (Exception ex)
            {
                MessageBox.Show("There was an error loading the modules. Please restart the application.", "Oops!");
                //return;
                CreateLogFiles.ErrorLog("Error: " + ex.Message);
                throw ex;
            }
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            MessageBox.Show("There was an error loading the modules. Please restart the application.", "Oops!");
            Application.Exit();
        }

        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            MessageBox.Show("There was an error loading the modules. Please restart the application.", "Oops!");
            Application.Exit();
        }
}

There's the full code in Main().Any help is appreciated. I'm running out of time, and getting a bit desparate. Thanks in advance!

+1  A: 

Try using AppDomain.UnhandledException event to log the error before app is closed.

Andrew Bezzub
+1  A: 

Hello, surrounding the Main you mean something like this?

    static void Main()
    {
        try
        {
            //code
        }
        catch (Exception e)
        {
            //code
        }
    }

If you do mean something like this, then try add thread exception handlers:

    static void Main()
    {

        // your event handler
        Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
        // your event handler
        AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
        // you try catch block and Application.Run
    }

And if this solution doesnt help you, you can always read about application errors in Windows events repository (control panel-> administration -> events).

Andrew
I looked through the events here's what the error reports: "Faulting application card scribe.exe, version 1.0.0.0, stamp 4bc30e74, faulting module kernel32.dll, version 5.1.2600.3541, stamp 49c4f751, debug? 0, fault address 0x00012a6b."There is no more information on this. I'll try out the exception handling you have provided, and tell you how it went. Thanks for responding!
Sreedevi J
Hi Andrew, Thanks for responding, but adding the code suggested did not help. The problem still exists, and the log also says the same thing as I posted yesterday. Can you think of anything else? I've been looking, but drawing blanks.Thanks!
Sreedevi J
External unmanaged errors can give you a big headache. I suggest you to log you App progress in logfile, you can write you own simple it's no big deal, and do something like this:SimpleLogger.WriteLine("File program.cs 1");Application.EnableVisualStyles();SimpleLogger.WriteLine("File program.cs 2");Application.SetCompatibleTextRenderingDefault(false);SimpleLogger.WriteLine("File program.cs 3");Application.Run(new Form1());etc, so you can define when error occure. And some of you code can be executed before Main (like init of Main static variables etc), so check them to.
Andrew
Yes, the tiring part is that I have no control over the unmanaged code, since it's proprietary to someone else. Logging the error also is not working, it seems that the Exception handlers are not getting called at all! I have surrounded all the code in some sort of try-catch, so I'm confused as to why not even one of them are firing.-Still pulling my hair out on this one...
Sreedevi J
Hi Andrew, I added the Main() code. Is there something amiss?
Sreedevi J
Will it fire error if no code in Main, and will it fire error if you start other template Win App, if this is third party library fault which you need to use then probably nothing you can do (if you template win app ll work), if this is Framework fault (template app won't work, then try to play with project properties like target framework, or reinstall / update framework libraries etc), and workaround is start app from script, or from another app (if it's not framework fault), script for example will start app 2 times, and app in the main will check if this it process already running.
Andrew
Thank you, Andrew. I'll work on it.
Sreedevi J
Thank you Andrew. It led me to find the error after a lot of running around in circles. Turns out there was also an issue with the 2.0 Framework, and it's compatibility with Oracle. Thanks!
Sreedevi J