繁体   English   中英

如何在C#程序退出时保持批处理文件运行?

[英]How to keep batch file running while C# program quit?

我写了一个C#代码,它将读取几个.xml文件(包含可执行文件,路径,参数。.xml文件包含一系列需要运行的可执行文件),并且将创建几个与.xml文件关联的批处理文件。 。

创建每个批处理文件后,程序将以C#代码执行每个批处理文件(当然使用cmd)。 问题是某些批处理文件将运行几天,并且保持C#程序运行会浪费内存/即时信息。 是否可以保持.bat文件运行并关闭C#代码? 假设我们不需要来自C#代码的任何错误报告。

下面是执行批处理文件的代码。

    //Execute BatchFile
    public static void ExecuteCommand(string command, string dummyPath)
    {
        int exitCode;
        ProcessStartInfo processInfo;
        Process process;

        processInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
        processInfo.WorkingDirectory = dummyPath;
        processInfo.CreateNoWindow = true;
        processInfo.UseShellExecute = false;
        processInfo.RedirectStandardError = true;
        processInfo.RedirectStandardOutput = true;

        process = Process.Start(processInfo);
        process.WaitForExit();

        string output = process.StandardOutput.ReadToEnd();
        string error = process.StandardError.ReadToEnd();

        exitCode = process.ExitCode;

        Console.WriteLine("output>>" + (String.IsNullOrEmpty(output) ? "(none)" : output));
        Console.WriteLine("error>>" + (String.IsNullOrEmpty(error) ? "(none)" : error));
        Console.WriteLine("ExitCode: " + exitCode.ToString(), "ExecuteCommand");
        process.Close();
    } 

如果我对您的理解正确,我认为有可能不需要.bat文件的任何反馈。 我已经在自己的机器上测试了这行代码,并且运行良好。

    static void Main(string[] args)
    {
        System.Diagnostics.Process.Start(@"d:\simple.bat");
    }

出于测试目的,我只需将PING www.google.com放在我的.bat文件中。

cmd批处理进程属于您的程序进程,主进程停止并且子进程将被杀死。

我认为您可以尝试在程序中创建Windows任务计划,在taski中批量运行,程序停止,任务计划仍将运行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM