Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6590903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:21:30+00:00 2026-05-25T17:21:30+00:00

I have a question about exception handling. To prevent the [YourProgram] has stopped working

  • 0

I have a question about exception handling.

To prevent the “[YourProgram] has stopped working” Windows dialog, I usually catch even unhandled exceptions this way:

In App.xaml.cs:

    protected override void OnStartup(StartupEventArgs e)
    {
        Application.Current.DispatcherUnhandledException += ProcessDispatcherException;
        AppDomain.CurrentDomain.UnhandledException += ProcessUnhandledException;
        // Blah blah blah... Performs a lot of loading operations...
        mainWindow.Show();
    }

and then

    private void ProcessUnhandledException(object o, UnhandledExceptionEventArgs e)
    {
        logException("An unhandled exception has been thrown\n"+(e.ExceptionObject as Exception).ToString(), e.ExceptionObject as Exception);
        Application.Current.Shutdown();
    }

Okay, I don’t have the Windows dialog. Now ideally I’d like to prevent this force closing scenario. The application I’m developing here has a startup time which lasts around 1 minute for the lightest users (most of them need to wait 2 or 3 minutes for launching it, it has to load a very large and complex data referential), so restarting it can cause trouble

I’d like to know about your “best practices” for this case. I am thinking about just re-creating a new window in the handler and re-show it anyway, so only the UI will be reinitialized to startup state, no other referential will be loaded, 2 – 3 minutes saved. Any other advices?

Oh and of course, this is the “extreme emergency case which should not be reached”, but unfortunately it is, mostly due to our dependencies to other systems managed by other branches of the company with who I don’t have any control or right to complain (yes, international companies can suck sometime), and it is not try/catchable in code 🙁

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T17:21:30+00:00Added an answer on May 25, 2026 at 5:21 pm

    I am assuming from what you are writing that you want your application to be mission critical, meaning, if anything occurs that makes it fail, it needs to be restarted automatically.

    The best way to accomplish this is to create a second watchdog process that will restart your application anytime it fails. You can then allow your application to quietly terminate itself when there is an unhandled exception, cleaning up whatever you can in your unhandled exception processor.

    The simplest way to implement a watchdog like this is to have a no-window process (e.g., a console app) with a background thread loop that periodically checks that your application is running by checking if a wait handle is locked. Something like this:

    // Declared in class
    object checkLocker = new object();
    bool mtStopCheck = false;
    
    // Thread loop
    bool stopCheck = false;
    while (stopCheck == false)
    {
        if (wait_handle_is_unlocked)
            restart_application();
        Thread.Sleep(1000);
    
        lock (checkLocker)
        {
            stopCheck = mtStopCheck;
        }
    }
    

    When you want to shut the watchdog down another thread does this:

    // Stop the watchdog thread so the watchdog app can shut down
    lock (checkLocker)
    {
        mtStopCheck = true;
    }
    

    Since you’d be running in the same terminal session you don’t need a global wait handle so no privilege issues on Vista/Windows 7.

    If the user closes the application and you don’t want it running again, you can send a signal to the watchdog process (e.g., with named pipes, or use a second kind of wait handle that you lock when you want the watchdog to suspend or shut down) that the app has been shut down and should not be restarted.

    You can launch your watchdog in the startup folder or some other automatic method, or, you can have your app launch it the first time it runs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question about how to catch the exception in the initialization list.
I have a question about handling exception. I have a Winform that uses a
I have a question about error/exception handling on the iPhone. I've read the documentation
This may be a ridiculous Java question about exception handling, but I have a
In fact I have Idea about the exception handling. But while facing the interview
I have a question about when to use an Exception or If/Else statement. In
I got a question about exception handling. I guess it may be quite easy
I have a couple questions about exceptions. 1) when you hit a catch block,
I have question about NSView: Imagine a Custom View where the mouseDown, mouseDrag and
I have question about normalization. Suppose I have an applications dealing with songs. First

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.