Program.cs 3.6 KB
Newer Older
1 2
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.

L
Larry Golding 已提交
3 4
using System;
using System.Collections.Generic;
5
using System.Diagnostics;
L
Larry Golding 已提交
6 7
using System.Linq;
using System.Reflection;
8
using System.Threading;
9 10
using CommandLine;

L
Larry Golding 已提交
11

12 13 14 15
namespace ProcessWatchdog
{
    internal sealed class Program
    {
16
        private Options _options;
17
        private TimeSpan _timeLimit;
18 19 20 21 22 23 24 25

        public Program(Options options)
        {
            _options = options;
        }

        private int Run()
        {
26
            if (_options.TimeLimit <= 0)
27
            {
28
                ConsoleUtils.LogError(Resources.ErrorInvalidTimeLimit, _options.TimeLimit);
29 30 31
                return 1;
            }

32 33
            _timeLimit = TimeSpan.FromSeconds(_options.TimeLimit);

34 35 36 37 38 39
            if (_options.PollingInterval <= 0)
            {
                ConsoleUtils.LogError(Resources.ErrorInvalidPollingInterval, _options.PollingInterval);
                return 1;
            }

40 41 42 43 44
            var processStartInfo = new ProcessStartInfo
            {
                FileName = _options.Executable,
                Arguments = _options.Arguments
            };
45

46
            Process parentProcess = Process.Start(processStartInfo);
47 48
            ProcDump procDump = new ProcDump(_options.ProcDumpPath, _options.OutputFolder);

49
            using (ProcessTracker processTracker = new ProcessTracker(parentProcess, procDump))
50
            {
51
                while (!processTracker.AllFinished)
52
                {
53
                    if (DateTime.Now - parentProcess.StartTime > _timeLimit)
54
                    {
55 56 57 58 59 60 61
                        ConsoleUtils.LogError(
                            Resources.ErrorProcessTimedOut,
                            _options.Executable,
                            parentProcess.Id,
                            _options.TimeLimit);

                        if (_options.Screenshot)
62
                        {
63
                            ScreenshotSaver.SaveScreen(_options.Executable, _options.OutputFolder);
64
                        }
65

66 67
                        processTracker.TerminateAll();
                        return 1;
68 69
                    }

70
                    Thread.Sleep(_options.PollingInterval);
71 72

                    processTracker.Update();
73
                }
74 75 76 77 78

                ConsoleUtils.LogMessage(
                    Resources.ProcessExited,
                    _options.Executable,
                    parentProcess.ExitTime - parentProcess.StartTime);
79
            }
80

81 82 83
            return 0;
        }

84 85
        private static void Main(string[] args)
        {
L
Larry Golding 已提交
86
            Banner();
87 88 89 90 91 92 93 94 95 96 97

            Parser.Default.ParseArguments<Options>(args)
                .MapResult(
                    options => Run(options),
                    err => 1);
        }

        private  static int Run(Options options)
        {
            var program = new Program(options);
            return program.Run();
L
Larry Golding 已提交
98 99 100 101 102 103 104 105
        }

        private static void Banner()
        {
            Assembly entryAssembly = Assembly.GetEntryAssembly();
            IEnumerable<Attribute> attributes = entryAssembly.GetCustomAttributes();

            string version = entryAssembly.GetName().Version.ToString();
106
            ConsoleUtils.LogMessage(Resources.Banner, Resources.ApplicationName, version);
L
Larry Golding 已提交
107 108 109 110

            var copyrightAttribute = attributes.Single(a => a is AssemblyCopyrightAttribute) as AssemblyCopyrightAttribute;
            string copyright = copyrightAttribute.Copyright;

111 112
            ConsoleUtils.LogMessage(copyright);
            ConsoleUtils.LogMessage(string.Empty);
113 114 115
        }
    }
}