秒表不会计算时间

时间:2016-10-04 20:23:12

标签: c# debugging time timer stopwatch

我为我的翻译做了一个简单的调试器。

除了一件事之外它完美无缺 - 无限循环检测系统不起作用。它应该询问用户是否要每20秒停止一次调试器,但没有任何反应。

以下是源代码:

using System;
using System.Diagnostics;
using System.Text;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;

namespace MOPL.Debugger
{
    class Debugger
    {
        static void Main(string[] args)
        {
            if (File.Exists("debug.log"))
            {
                File.Delete("debug.log");
            }
            handler = new ConsoleEventDelegate(ConsoleEventCallback);
            SetConsoleCtrlHandler(handler, true);
            bool stop = true, five = false;
            if (Console.IsOutputRedirected || Console.IsErrorRedirected)
            {
                Console.WriteLine("This is a debugger, it shall not get debugged.");
                return;
            }
            if (Console.IsInputRedirected)
            {
                Console.WriteLine("Control is not yet implemented.");
            }
            if (args.Count() < 1)
            {
                Console.WriteLine("Invalid file.");
                return;
            }
            if (args.Count() > 1)
            {
                if (args[1].ToUpper() == "NOSTOP")
                {
                    stop = false;
                }
                else
                {
                    stop = true;
                }
            }
            string exe = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\" + "MOPL.RunHelper.exe";
            startInfo = new ProcessStartInfo(exe, args[0]);
            if (!File.Exists(exe))
            {
                Console.WriteLine("The run helper was not found!");
                return;
            }
            args[0] = args[0].Replace(":", "@");
            args[0] = "deb:" + args[0];
            startInfo.UseShellExecute = false;
            startInfo.RedirectStandardError = true;
            startInfo.RedirectStandardInput = true;
            startInfo.RedirectStandardOutput = true;
            App = Process.Start(startInfo);
            try
            {
                Stopwatch s = new Stopwatch();
                s.Stop();
                int i = 0;
                App.Start();
                Console.Clear();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.BackgroundColor = ConsoleColor.Black;
                Console.WriteLine("Debugging..." + "\n" + "Make sure that there is no infinite loop!");
                while (!App.HasExited)
                {
                    if ((s.Elapsed > TimeSpan.FromSeconds(20)))
                    {
                        Console.WriteLine();
                        Console.ForegroundColor = ConsoleColor.Red;
                        if (five == false)
                        {
                            Console.WriteLine("The debugger has been running for 20 secods.");
                        }
                        else
                        {
                            Console.WriteLine("20 more seconds have passed.");
                        }
                        Console.WriteLine("You may have made an infinite loop!");
                        stopdbg = true;
                        five = true;
                        s.Restart();
                    }
                    Errors.Append(App.StandardError.ReadToEnd());
                    Output.Append(App.StandardOutput.ReadToEnd());
                    App.StandardInput.WriteLine("0");
                    i++;
                    System.Threading.Thread.Sleep(75);
                    if (stopdbg)
                    {
                        stopdbg = true;
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine();
                        Console.WriteLine("Do you want to stop debugging? [Y/N]");
                        bool quit = Ask();
                        if (quit == true)

                            break;
                    }
                }
                App.Close();
                File.AppendAllText("debugger_errors.log", Errors.ToString());
                File.AppendAllText("debugger_output.log", Output.ToString());
                Safe = true;
                Console.Clear();
                Console.WriteLine();
                Console.ResetColor();
                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Red;
                Console.BackgroundColor = ConsoleColor.Black;
                Console.WriteLine("Data sent to debugger:");
                Console.ForegroundColor = ConsoleColor.Black;
                Console.BackgroundColor = ConsoleColor.DarkRed;
                Console.WriteLine("{0}", Errors.ToString());
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.BackgroundColor = ConsoleColor.Black;
                Console.WriteLine();
                Console.WriteLine("Full output:");
                Console.BackgroundColor = ConsoleColor.DarkYellow;
                Console.ForegroundColor = ConsoleColor.Black;
                Console.WriteLine("{0}", Output.ToString());
                if (File.Exists("debug.log"))
                {
                    try
                    {
                        Console.WriteLine();
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.BackgroundColor = ConsoleColor.Black;
                        Console.WriteLine("Data sent to debug log:");
                        Console.BackgroundColor = ConsoleColor.Black;
                        Console.BackgroundColor = ConsoleColor.DarkGreen;
                        string[] lines = File.ReadAllLines("debug.log");
                        foreach (string line in lines)
                        {
                            Console.WriteLine(line);
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Failed to open debug.log ({0})", e);
                    };
                }
                Console.ResetColor();
                if (stop) Console.ReadKey(true);
            }
            catch (Exception e)
            {
                App.Close();
                Console.WriteLine("Debugger has crashed with exception: {0}", e);
                if (stop) Console.ReadKey(true);
            }
        }

        private static bool Ask()
        {
            while (true)
            {
                ConsoleKeyInfo result = Console.ReadKey(true);
                if ((result.KeyChar == 'Y') || (result.KeyChar == 'y'))
                {
                    return true;
                }
                else if ((result.KeyChar == 'N') || (result.KeyChar == 'n'))
                {
                    return false;
                }
            }
        }
        static bool ConsoleEventCallback(int eventType)
        {
            if (eventType == 2)
            {
                if (!Safe)
                {
                    App.Close();
                    File.AppendAllText("debugger_errors.log", Errors.ToString());
                    File.AppendAllText("debugger_output.log", Output.ToString());
                    Safe = true;
                    Console.WriteLine("The console will close, check the log files.");
                }
                return true;
            }
            return false;
        }
        static ConsoleEventDelegate handler;
        static StringBuilder Errors = new StringBuilder();
        static StringBuilder Output = new StringBuilder();
        private static bool stopdbg = false;
        static Process App;
        static ProcessStartInfo startInfo;
        private static bool Safe;

        private delegate bool ConsoleEventDelegate(int eventType);
        [DllImport("kernel32.dll", SetLastError = true)]
        private static extern bool SetConsoleCtrlHandler(ConsoleEventDelegate callback, bool add);
    }
}

1 个答案:

答案 0 :(得分:2)

尝试更改

Stopwatch s = new Stopwatch();
s.Stop();

Stopwatch s = new Stopwatch();
s.Start();