Advertisement
Guest User

Untitled

a guest
Nov 26th, 2017
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.62 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3.  
  4. namespace StackOverflowTest
  5. {
  6.     class Program
  7.     {
  8.         static bool fault = false;
  9.         static Exception ex;
  10.  
  11.         static void Main(string[] args)
  12.         {
  13.             Thread t = new Thread(Run);
  14.             t.Start();
  15.             t.Join();
  16.             if(fault)
  17.             {
  18.                 Console.WriteLine("fault occured:");
  19.                 Console.WriteLine(ex);
  20.             }
  21.             else
  22.             {
  23.                 Console.WriteLine("no fault");
  24.             }
  25.             Console.Read();
  26.         }
  27.  
  28.         static void Run()
  29.         {
  30.             try
  31.             {
  32.                 Execute();
  33.             }
  34.             catch(Exception e)
  35.             {
  36.                 ex = e;
  37.                 fault = true;
  38.             }
  39.         }
  40.  
  41.         static void Execute()
  42.         {
  43.             WaitOne();
  44.         }
  45.  
  46.         static bool WaitOne(bool killProcessOnInterrupt = false, bool throwOnInterrupt = false)
  47.         {
  48.             try
  49.             {
  50.                 return WaitOne();
  51.             }
  52.             catch(ThreadInterruptedException e)
  53.             {
  54.                 /*if(killProcessOnInterrupt)
  55.                 {
  56.                     Process.Kill();
  57.                 }
  58.                 if(throwOnInterrupt)
  59.                 {
  60.                     ExceptionDispatchInfo.Capture(e).Throw(); //this will rethrow the exception with stack trace intact
  61.                     throw; //visual studio doesnt know above method will throw, so this has to be here
  62.                 }*/
  63.             }
  64.             return false;
  65.         }
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement