Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- while (true)
- {
- //keep checking if timer expired or app closed externally (ie. by user)
- if (dtEndTime <= DateTime.Now || p.HasExited)
- {
- if (!p.HasExited)
- p.Kill();
- break;
- }
- System.Threading.Thread.Sleep(500);
- }
- try
- {
- if(!p.HasExited)
- {
- p.Kill();
- }
- break;
- }
- catch(Exception ex)
- {
- System.Diagnostics.Trace.WriteLine(String.Format("Could not kill process {0}, exception {1}", p.ToString(), ex.ToString()));
- }
- using System;
- public class MyClass
- {
- public static void Main()
- {
- System.AppDomain.CurrentDomain.UnhandledException += MyExceptionHandler;
- System.Threading.ThreadPool.QueueUserWorkItem(DoWork);
- Console.ReadLine();
- }
- private static void DoWork(object state)
- {
- throw new ApplicationException("Test");
- }
- private static void MyExceptionHandler(object sender, System.UnhandledExceptionEventArgs e)
- {
- // get the message
- System.Exception exception = e.ExceptionObject as System.Exception;
- Console.WriteLine("Unhandled Exception Detected");
- if(exception != null)
- Console.WriteLine("Message: {0}", exception.Message);
- // for this console app, hold the window open until I press enter
- Console.ReadLine();
- }
- }
- p.WaitForInputIdle(10000);
- am.hWnd = p.MainWindowHandle;
- while (true)
- {
- //keep checking if timer expired or app closed externally (ie. by user)
- if (dtEndTime <= DateTime.Now || p.HasExited) {
- try {
- if (!p.HasExited) // if the app hasn't already exitted...
- {
- if (!p.CloseMainWindow()) // did message get sent?
- {
- if (!p.HasExited) //has app closed yet?
- {
- p.Kill(); // force app to exit
- p.WaitForExit(2000); // a few moments for app to shut down
- }
- }
- p.Close(); // free resources
- }
- }
- catch { // blah blah }
- break;
- }
- System.Threading.Thread.Sleep(500);
- }
Add Comment
Please, Sign In to add comment