[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

On Crash, Restart Application

Posted on 2009-03-31
4
Medium Priority
?
722 Views
Last Modified: 2013-12-17
I have a crawler I wrote that randomly crashes (crash comes from a window dll). When the program crashes, a window pops up saying "debug, send error report, etc..". I want to prevent that window from opening and restart the application, I am including the code I am currently attempting to use, but I have had zero luck, any ideas on how I can accomplish this?
public static Form1 form1;
        public static System.Threading.Timer tmr;
        public static System.Threading.Timer tmr2;
 
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            bool b = true;
 
            while (b == true)
            {
                try
                {
                    tmr = new System.Threading.Timer(new System.Threading.TimerCallback(onTimeout), null, 5000, 5000);
                    form1 = new Form1();
                    Application.Run(form1);
                }
                catch (Exception ex)
                {
 
                }
 
                System.Threading.Thread.Sleep(30000);
            }
        }
 
        
        public static void onTimeout(object obj)
        {
            tmr2 = new System.Threading.Timer(new System.Threading.TimerCallback(onTimeoutTimeout), null, 1000, System.Threading.Timeout.Infinite);
            bool a = form1.IsAlive();
            if (a == true)
            {
                tmr2.Dispose();
            }
        }
 
        public static void onTimeoutTimeout(object obj)
        {            
            form1.Dispose();
        }

Open in new window

0
Comment
Question by:addicktz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 29

Assisted Solution

by:Gautham Janardhan
Gautham Janardhan earned 1000 total points
ID: 24036257
which frame work are u using ? 3.0 ?
0
 
LVL 10

Assisted Solution

by:margajet24
margajet24 earned 200 total points
ID: 24036514
the application crashes because some exception were not handled.

If you are able to handle those exceptions, your app will not crash.

you can try to to put a try-catch block handling "Exception" in the catch. This would ensure that all kinds of exception would be caught/handled..
0
 
LVL 18

Accepted Solution

by:
philipjonathan earned 800 total points
ID: 24037379
Try adding this to the top of your Main:

static void Main()
{
   AppDomain currentDomain = AppDomain.CurrentDomain;
   currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);

   ...
}


And add this static method:

   static void MyHandler(object sender, UnhandledExceptionEventArgs args) {
      Exception e = (Exception) args.ExceptionObject;
      Console.WriteLine("MyHandler caught : " + e.Message);
   }

0
 
LVL 29

Assisted Solution

by:Gautham Janardhan
Gautham Janardhan earned 1000 total points
ID: 24045902
u can use the code posted above but
in 1.1 -> u can actaully stop the application from shutting down
in 2.0 and above -> u cant stop the application from closing but u can write code in the MyHandler method to start the application again.

static void MyHandler(object sender, UnhandledExceptionEventArgs args) {
      Exception e = (Exception) args.ExceptionObject;
      Console.WriteLine("MyHandler caught : " + e.Message);
      System.Diagnostics.Process.Start(System.Reflection.Assembly.GetEntryAssembly().Location);
   }

Open in new window

0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question