Browse Source

make the watchdog bite harder

Florian Mueller 7 years ago
parent
commit
0c20d30a53

BIN
.vs/bbiwarg/v14/.suo


+ 35 - 16
bbiwarg/Input/InputProviding/InputProviderIntel.cs

@@ -3,6 +3,7 @@ using Emgu.CV;
 using Emgu.CV.Structure;
 using System.Timers;
 using System.Threading;
+using System.Diagnostics;
 
 namespace BBIWARG.Input.InputProviding
 {
@@ -55,21 +56,27 @@ namespace BBIWARG.Input.InputProviding
         public void initialize()
         {
             CurrentFrameID = 0;
-
-            watchdogTimer = new System.Threading.Timer(Timer_Elapsed, null, 1000, 1000);
+            watchdogTimer = new System.Threading.Timer(Timer_Elapsed, null, 2000, 1000);
         }
 
+        private bool errorstate = false;
+
         private void Timer_Elapsed(object state)
         {
             checkExitErrorstate();
 
-            if (wrapper != null)
-                wrapper.errorstate = true;
+            errorstate = true;
         }
 
+        int startId = 0;
+        Stopwatch startTimeWatch;
+
         public void start()
         {
-            domain = System.AppDomain.CreateDomain(System.Guid.NewGuid().ToString());
+            Console.WriteLine("starting new AppDomain (" + startId +")...");
+            domain = System.AppDomain.CreateDomain("AppDomain-" + startId);
+
+            startId++;
 
             wrapper = (IntelCameraWrapper) domain.CreateInstanceAndUnwrap(typeof(IntelCameraWrapper).Assembly.GetName().ToString(), typeof(IntelCameraWrapper).FullName);
             wrapper.init(this);
@@ -80,30 +87,45 @@ namespace BBIWARG.Input.InputProviding
                 DeviceStartedEvent(this, new EventArgs());
                 DeviceStartedEvent = null; //only notify once...
             }
-               
 
+            scheduledForRestart = false;
+            startTimeWatch = Stopwatch.StartNew();
             wrapper.run();
 
             checkExitErrorstate();
         }
 
+        private bool scheduledForRestart = false;
+
         private void checkExitErrorstate()
         {
-            if (wrapper.errorstate)
+            if (errorstate && !scheduledForRestart)
             {
+                if (startTimeWatch != null && startTimeWatch.ElapsedMilliseconds < 5000) {
+                    Console.WriteLine("...");
+                    return;
+                }
+                    
+                startTimeWatch.Stop();
+
                 //we have crashed. Kill the app domain and try again.
-                Console.WriteLine("Killing AppDomain...");
+                Console.WriteLine("Killing AppDomain (" + domain.FriendlyName + ")...");
+                scheduledForRestart = true;
+                //wrapper.terminated = true;
+
                 try
                 {
                     System.AppDomain.Unload(domain);
-                    Thread.Sleep(5000);
                 }
                 catch (Exception)
                 {
-                    Console.WriteLine("Could not unload app domain... Still trying to start again...");
+                    Console.WriteLine("Could not unload app domain. We will just wait a moment and try to start a new one.");
                 }
-                
-                Console.WriteLine("Starting again...");
+
+                domain = null;
+                wrapper = null;
+
+                Thread.Sleep(5000);
                 start();
             }
         }
@@ -118,13 +140,10 @@ namespace BBIWARG.Input.InputProviding
             return NewFrameEvent != null;
         }
 
-        internal void killAndRestart()
-        {
-            throw new NotImplementedException();
-        }
 
         internal void newFrame(int currentFrameID, Image<Gray, ushort> dImg)
         {
+            errorstate = false;
             NewFrameEvent(this, new NewFrameEventArgs(currentFrameID, dImg));
         }
     }

+ 11 - 4
bbiwarg/Input/InputProviding/IntelCameraHandler.cs

@@ -22,7 +22,8 @@ namespace BBIWARG.Input.InputProviding
         private dynamic EverythingFine;
         private MethodInfo acquireAccessMethod;
         private object[] methodParams;
-        public bool errorstate = false;
+        //public bool errorstate = false;
+        public bool terminated = false;
 
         public IntelCameraWrapper()
         {
@@ -77,17 +78,23 @@ namespace BBIWARG.Input.InputProviding
         {
             while (inputProviderIntel.IsActive)
             {
-                errorstate = false;
+                if (terminated)
+                    return;
+
+                //errorstate = false;
 
                 var status = senseManager.AcquireFrame(true);
 
                 if (status != EverythingFine)
                 {
                     Console.WriteLine("Found crash inside CameraHandler...");
-                    errorstate = true;
-                    break;
+                    //errorstate = true;
+                    return;
                 }
 
+                if (terminated)
+                    return;
+
                 if (inputProviderIntel.hasNewFrameEvent())
                 {