1

I wrote a small WinForms application in C# with Visual Studio 2010.

It worked quite well in my environment, but I noticed that I need to run the application at the customer site on a Windows Server 2003 SP2 server with the .NET Framework 2.0 installed.

I then built the application with .NET Framework 2.0 as the target framework. Again the application ran perfectly fine on my computer.

At the customer server I noticed a strange behaviour. After starting the application, a black window opens for half a second and closes afterwards. I don't get an error message.

I discovered that the application doesn't even enter the main function.

How can this be debugged further, and/or solved?

p.campbell
  • 95,348
  • 63
  • 249
  • 319
chris
  • 555
  • 2
  • 5
  • 14

2 Answers2

2

What you need to do is subscribe to two events: UnhandledException and AssemblyResolve. The first is triggered on ... an unhandled exception (of course). The second fires when .NET encounters an assembly reference that it cannot resolve. One or both of these will help you trap your error on the target machine.

 [STAThread]
    static void Main()
    {
        AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
        AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }

    static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        string message = string.Format("Program encountered an unhandled Exception: {0}", e.ExceptionObject);
        MessageBox.Show(message);
    }

    static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
    {
        string message = string.Format("Assembly cannot be located!: {0}", args.Name);
        MessageBox.Show(message);
        return null;
    }

You should registered these two event handlers at the very beginning on your code, as shown.

Hope this helps!

Glenn Ferrie
  • 9,875
  • 3
  • 38
  • 69
  • Thank you for your Help. It really helped! An assembly could not be accessed! I read the explanation for the issue here: http://stackoverflow.com/questions/5994216/c-change-framework-error ! – chris Jul 05 '11 at 12:52
0

There should be an application start event. I'm now years passed windows programming, but I remember that Application had some start event and end event and stuff like that. You can search for those events, create handlers for them, and in those handlers, try to write to a text file to kind of trace your application. Also if you can post any code, it would be useful.

Saeed Neamati
  • 34,403
  • 40
  • 131
  • 186