예제 #1
0
 public void Run(object aCommandReceived)
 {
     LastRun = DateTime.Now;
     CommandReceivedDelegate CommandReceived = (CommandReceivedDelegate) aCommandReceived;
     for (Running = true; Running; )
     {
         itsPipes = new Pipes(
             new System.IO.Pipes.NamedPipeClientStream(".", "XervBackup.Pipe.Out", System.IO.Pipes.PipeDirection.In),
             new System.IO.Pipes.NamedPipeClientStream(".", "XervBackup.Pipe.In", System.IO.Pipes.PipeDirection.Out));
         while (Running && !itsPipes.Out.IsConnected)
         {
             try { itsPipes.Out.Connect(10000); } //1000); }
             catch { }
         }
         itsPipes.In.Connect();
         itsWriter = new System.IO.StreamWriter(itsPipes.Out);
         itsWriter.AutoFlush = true; // !!
         Debug.WriteLine("CONNECTED");
         if (ConnectedChanged != null) ConnectedChanged(IsConnected);
         System.Media.SystemSounds.Beep.Play();
         using (System.Timers.Timer KeepAlive = new System.Timers.Timer(1000))
         using (System.IO.StreamReader Reader = new System.IO.StreamReader(itsPipes.In))
         {
             KeepAlive.Elapsed += new System.Timers.ElapsedEventHandler(
                 delegate(object sender, System.Timers.ElapsedEventArgs e)
                 {
                     SendCommand(string.Empty);  // Send empty keepalive
                 });
             while (Running && itsPipes.In.IsConnected)
             {
                 try
                 {
                     string Line = Reader.ReadLine();
                     if (!string.IsNullOrEmpty(Line)) // Keepalive is empty
                         CommandReceived(Line);
                 }
                 catch (Exception Ex)
                 {
                     Debug.WriteLine(Ex);
                 }
             }
             itsPipes.Close();
         }
         Debug.WriteLine("Exiting pipe thread");
         if (ConnectedChanged != null) ConnectedChanged(IsConnected);
     }
 }