static Publish AutodetectActivePublish()
 {
     try
     {
         foreach (string s in Publish.GetAllPublishNames(pubRoot))
         {
             Publish p = Publish.Load(s, pubRoot);
             if (p.IsCommandRunning())
             {
                 return(p);
             }
         }
     }
     catch
     { }
     return(null);
 }
        public static void MainLoop()
        {
            comm.GotStartPublishMessage      += new EventHandler <WatchdogMessageEventArgs <StartPublishMessage> >(comm_GotStartPublishMessage);
            comm.GotStopPublishMessage       += new EventHandler <WatchdogMessageEventArgs <StopPublishMessage> >(comm_GotStopPublishMessage);
            comm.GotWatchdogTerminateMessage += new EventHandler <WatchdogMessageEventArgs <WatchdogTerminateMessage> >(comm_GotWatchdogTerminateMessage);
            comm.GotCommandMessage           += new EventHandler <WatchdogMessageEventArgs <CommandMessage> >(comm_GotCommandMessage);

            Thread t = new Thread(new ThreadStart(StatusThread));

            t.Name = "Watchdog Status Thread";
            t.Start();
            Publish autoP = AutodetectActivePublish();

            if (autoP != null)
            {
                StartPublish(autoP.name);
            }
#if !SERVICE
            while (running)
            {
                string s = Console.ReadLine();
                if (s == null)
                {
                    continue;
                }
                if (s.StartsWith("load") && s.Split(" ".ToCharArray(), StringSplitOptions.None).Length > 1)                 //load a publish
                {
                    Trace.WriteLine("Loading " + s.Substring(s.IndexOf(" ") + 1));
                    curPublish = Publish.Load(s.Substring(s.IndexOf(" ") + 1), pubRoot);
                }
                if (s.StartsWith("show all"))
                {
                    foreach (string p in Publish.GetAllPublishNames(pubRoot))
                    {
                        Trace.WriteLine(p);
                    }
                }
                if (s.StartsWith("curpub"))
                {
                    if (curPublish == null)
                    {
                        Trace.WriteLine("No Publish Loaded.");
                    }
                    else
                    {
                        curPublish.PrintDebug();
                        Trace.WriteLine("Running: " + curPublish.IsCommandRunning());
                    }
                }
                if (s.StartsWith("start"))
                {
                    if (curPublish == null)
                    {
                        Trace.WriteLine("No publish loaded.");
                    }
                    else
                    {
                        curPublish.Start(pubRoot);
                    }
                }

                if (s.StartsWith("wdon"))
                {
                    if (curPublish == null)
                    {
                        Trace.WriteLine("No publish loaded.");
                    }
                    else
                    {
                        Trace.WriteLine("Enabling Watchdog...");
                        curPublish.watchdogAutoRestart = true;
                    }
                }

                if (s.StartsWith("wdoff"))
                {
                    if (curPublish == null)
                    {
                        Trace.WriteLine("No publish loaded.");
                    }
                    else
                    {
                        Trace.WriteLine("Disabling Watchdog...");
                        curPublish.watchdogAutoRestart = false;
                    }
                }
                if (s.StartsWith("list"))
                {
                    Process[]      parr             = Process.GetProcesses();
                    List <Process> runningProcesses = new List <Process>(parr);
                    foreach (Process filename in runningProcesses)
                    {
                        Trace.WriteLine(filename.ProcessName);
                    }
                }
            }
            t.Abort();
            traceText.Flush();
#endif
        }