aboutsummaryrefslogtreecommitdiff
path: root/Emby.Common.Implementations/Diagnostics/CommonProcess.cs
blob: afd30bc47fe5ae40b2875adfbb0e13baae5c6221 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using MediaBrowser.Model.Diagnostics;

namespace Emby.Common.Implementations.Diagnostics
{
    public class CommonProcess : IProcess
    {
        public event EventHandler Exited;

        private readonly ProcessOptions _options;
        private readonly Process _process;

        public CommonProcess(ProcessOptions options)
        {
            _options = options;

            var startInfo = new ProcessStartInfo
            {
                Arguments = options.Arguments,
                FileName = options.FileName,
                WorkingDirectory = options.WorkingDirectory,
                UseShellExecute = options.UseShellExecute,
                CreateNoWindow = options.CreateNoWindow,
                RedirectStandardError = options.RedirectStandardError,
                RedirectStandardInput = options.RedirectStandardInput,
                RedirectStandardOutput = options.RedirectStandardOutput
            };

            startInfo.ErrorDialog = options.ErrorDialog;

            if (options.IsHidden)
            {
                startInfo.WindowStyle = ProcessWindowStyle.Hidden;
            }

            _process = new Process
            {
                StartInfo = startInfo
            };

            if (options.EnableRaisingEvents)
            {
                _process.EnableRaisingEvents = true;
                _process.Exited += _process_Exited;
            }
        }

        private void _process_Exited(object sender, EventArgs e)
        {
            if (Exited != null)
            {
                Exited(this, e);
            }
        }

        public ProcessOptions StartInfo
        {
            get { return _options; }
        }

        public StreamWriter StandardInput
        {
            get { return _process.StandardInput; }
        }

        public StreamReader StandardError
        {
            get { return _process.StandardError; }
        }

        public StreamReader StandardOutput
        {
            get { return _process.StandardOutput; }
        }

        public int ExitCode
        {
            get { return _process.ExitCode; }
        }

        public void Start()
        {
            _process.Start();
        }

        public void Kill()
        {
            _process.Kill();
        }

        public bool WaitForExit(int timeMs)
        {
            return _process.WaitForExit(timeMs);
        }

        public Task<bool> WaitForExitAsync(int timeMs)
        {
            return Task.FromResult(_process.WaitForExit(timeMs));
        }

        public void Dispose()
        {
            _process.Dispose();
        }
    }
}