Re: How to determine if calling app is window or console

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Jay B. Harlow [MVP - Outlook] (Jay_Harlow_MVP_at_msn.com)
Date: 07/20/04


Date: Mon, 19 Jul 2004 21:48:46 -0500

Dean,
In addition to Tom's comments.

Depending on the type of application you are creating, .NET has three
different global exception handlers.

For ASP.NET look at:
    System.Web.HttpApplication.Error event
    Normally placed in your Global.asax file.

For console applications look at:
    System.AppDomain.UnhandledException event
    Use AddHandler in your Sub Main.

For Windows Forms look at:
    System.Windows.Forms.Application.ThreadException event
    Use AddHandler in your Sub Main.

It can be beneficial to combine the above global handlers in your app, as
well as wrap your Sub Main in a try catch itself.

There is an article in the June 2004 MSDN Magazine that shows how to
implement the global exception handling in .NET that explains why & when you
use multiple of the above handlers...

http://msdn.microsoft.com/msdnmag/issues/04/06/NET/default.aspx

For example: In my Windows Forms apps I would have a handler attached to the
Application.ThreadException event, plus a Try/Catch in my Main. The
Try/Catch in Main only catches exceptions if the constructor of the MainForm
raises an exception, the Application.ThreadException handler will catch all
uncaught exceptions from any form/control event handlers.

In your example: for my Windows Console application I would only handle the
AppDomain.UnhandledException event, possible using the Console API to query
the user, and send the results. For a Windows Forms application I would
handle at least the Application.ThreadException and possible the
AppDomain.UnhandledException (based on the info in the above link) using a
Windows Form to query the user and send the results. For a Windows Service I
would not query the user, instead simply logging & sending the info...

Because the event handlers are local to the application they would know what
type of application it is...

In either case I would log the information also, in case physically sending
is not an option.

Hope this helps
Jay

"Dean Slindee" <slindee@charter.net> wrote in message
news:10fot48kvuf5pae@corp.supernews.com...
> I have a exception handling class that could be called from either a
windows
> project app or a console project app. Is there any way for this class to
> determine which type of app called it without sending an window/console
> parameter from either app?
>
> Thanks,
> Dean Slindee
>
>



Relevant Pages

  • Re: Windows Form Level Exception
    ... No, there's nothing like that in Windows Forms, as far as I know. ... you should think of such global handlers only as of a "safety ... My recommendation is to have exception handlers everywhere an exception ... displaying error messages, but catch errors as close to the place they ...
    (microsoft.public.dotnet.framework)
  • Re: Tomcat und SapJco 3
    ... die web.xml der App ein touch macht, ... Tomcat unter Windows? ... Letzte Exception war folgende: ...
    (de.comp.lang.java)
  • Re: MessageBox.Show is showing BEHIND the application
    ... global exception handler with a single try/catch/MessageBox that is able to ... log the exception & show it to the user. ... It can be beneficial to combine the above global handlers in your app, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Exceptions, not being caught outside of the IDE!!!
    ... I think a change to the way the App start's is ... > different global exception handlers. ... In my Windows Forms apps I would have a handler attached to ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Exceptions, not being caught outside of the IDE!!!
    ... I think a change to the way the App start's is ... > different global exception handlers. ... In my Windows Forms apps I would have a handler attached to ...
    (microsoft.public.dotnet.framework)