c# – Dotnet core 2进程以超时开始

我有一个方法从dotnet core 2中的c#代码开始处理.这个方法如下:internal static string[] RunCommand(string filename, string args, string workingDirectory = null){var proc = new Process{StartInfo = new Pro...

我有一个方法从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();
    }

但我仍然有一些徘徊的过程.由于多线程进程的调试非常困难,导致这种情况的情况对我来说是未知的,您是否知道为什么某些进程应该从我的陷阱中获取?

解决方法:

首先,我需要对缺少的信息做一些假设:(i)您在Windows上运行代码,(ii)您的流程没有图形用户界面(窗口).有两种推荐的方法可以阻止Windows进程,其中首选的选择取决于进程的类型:(i)GUI进程,(ii)非GUI进程.在第二种情况下,您应该调用Process.Kill()方法以获得可靠的进程退出,紧接着是Process.WaitForExit(). Microsoft文档声明:

Kill is the only way to terminate processes that do not have graphical interfaces.

它不会减轻您清理进程资源(Dispose或Close调用),但是,此任务在某种程度上与Process.Kill()正交,根据文档可能会导致:

Data edited by the process or resources allocated to the process can be lost if you call Kill.

可能会抛出许多不同的异常,因此需要一些基本的异常处理代码.

本文标题为:c# – Dotnet core 2进程以超时开始

基础教程推荐