aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Tango.PPC.Common/WatchDog/WatchDogServer.cs
blob: 77bca2e3019f377627a5572e9790b1a7486c4614 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Tango.PPC.Common.WatchDog
{
    public class WatchDogServer
    {
        private NamedPipeServerStream _server;
        private Thread _thread;

        public bool IsStarted { get; private set; }

        public void Start()
        {
            _thread = new Thread(ThreadMethod);
            _thread.IsBackground = true;
            _thread.Start();
        }

        private void ThreadMethod()
        {
            IsStarted = true;

            try
            {
                _server = new NamedPipeServerStream("Tango_Watch_Dog_Pipe");
                _server.WaitForConnection();
                StreamReader reader = new StreamReader(_server);
                StreamWriter writer = new StreamWriter(_server);

                while (IsStarted)
                {
                    var line = reader.ReadLine();
                    writer.WriteLine(line);
                    writer.Flush();
                }
            }
            catch
            {
                IsStarted = false;
            }
        }

        public void Stop()
        {
            _server.Dispose();
        }
    }
}