Dotnet Core 2启动进程超时问题

4

我有一个方法可以在我的dotnet core 2 c#代码中启动进程。这个方法如下:

    internal static string[] RunCommand(string filename, string args, string workingDirectory = null)
    {
        var proc = new Process
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = filename,
                Arguments = args,
                UseShellExecute = false,
                RedirectStandardOutput = true,
                //WindowStyle = ProcessWindowStyle.Hidden
            }
        };

        if (workingDirectory != null)
        {
            proc.StartInfo.WorkingDirectory = workingDirectory;
        }

        //Console.WriteLine(proc.StartInfo.FileName + " " + proc.StartInfo.Arguments);

        List<string> lines = new List<string>();

        proc.Start();
        while (!proc.StandardOutput.EndOfStream)
        {
            string line = proc.StandardOutput.ReadLine();
            if (!string.IsNullOrEmpty(line))
            {

                lines.Add(line);
            }
        }
        proc.Dispose();

        return lines.ToArray();
    }

问题在于某些已启动的进程陷入循环,导致我的VPS出现问题。
那么问题是,有没有办法设置进程的截止时间?
更新
根据“Jacek Blaszczynski”的建议,我尝试了下面的代码:
    internal static string[] RunCommand(string filename, string args, string workingDirectory = null, int timeoutInSeconds = 60)
    {
        var proc = new Process
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = filename,
                Arguments = args,
                UseShellExecute = false,
                RedirectStandardOutput = true,
                //WindowStyle = ProcessWindowStyle.Hidden
            }
        };

        if (workingDirectory != null)
        {
            proc.StartInfo.WorkingDirectory = workingDirectory;
        }

        //Console.WriteLine(proc.StartInfo.FileName + " " + proc.StartInfo.Arguments);

        List<string> lines = new List<string>();

        bool isKilled = false;

        new Thread(() =>
        {
            Thread.CurrentThread.IsBackground = true;
            Thread.Sleep(timeoutInSeconds * 1000);
            try
            {
                if (proc != null && !proc.HasExited)
                {
                    isKilled = true;
                    proc.Kill();
                    Console.WriteLine("Annoying process killed.");
                }
            }
            catch
            {
                // just let it go
            }
        }).Start();

        try
        {
            proc.Start();
            while (!proc.StandardOutput.EndOfStream)
            {
                string line = proc.StandardOutput.ReadLine();
                if (!string.IsNullOrEmpty(line))
                {

                    lines.Add(line);
                }
            }
            proc.Dispose();
        }
        catch
        {
            // just look what happens
        }

        return isKilled ? new string[] { "" } : lines.ToArray();
    }

我有一些进程漫游的问题。由于多线程进程的调试非常困难,而且导致这种情况的原因我并不清楚,您有任何想法吗?为什么有些进程会从我的陷阱中逃脱?

1个回答

0

首先,我需要对缺失的信息做出一些假设:(i) 您在 Windows 上运行代码,(ii) 您的进程没有图形用户界面(窗口)。有两种推荐的方法来停止 Windows 进程,优选的选择取决于进程的类型:(i) GUI 进程,(ii) 非 GUI 进程。在第二种情况下,您应该调用 Process.Kill() 方法,以便在可靠的过程退出后立即跟随 Process.WaitForExit()。根据 Microsoft 文档:

Kill 是终止没有图形界面的进程的唯一方法。

不过,这并不意味着您可以忽略清理进程资源(调用 DisposeClose),但是,这个任务与 Process.Kill() 被认为是正交的,根据文档可能会导致:

如果您调用 Kill,则可能丢失由进程编辑的数据或分配给进程的资源。

有很多不同的异常可能被抛出,因此需要一些不只是基本的异常处理代码。


那么我应该如何在我的代码中使用您的建议?我应该如何检查并行线程是否已达到截止日期,以及如何从该并行线程中终止进程(或其他任何操作)?您能否请更明确地解释一下? - ConductedClever

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接