C# 中的批处理

发布于 2024-11-07 14:59:09 字数 611 浏览 0 评论 0原文

我有一个 Java 批处理进程,我在 C# 中使用它来运行进程。我想要一个测试用例来检查批处理是否正在运行。

我将批处理过程用作:

void batch_process(string process)
{
    try
    {
        string strFilePath = Path.Combine(batch_process_path, process);
        ProcessStartInfo psi = new ProcessStartInfo(strFilePath);
        psi.RedirectStandardOutput = true;
        psi.RedirectStandardError = true;
        psi.WorkingDirectory = batch_process_path;
        psi.WindowStyle = ProcessWindowStyle.Hidden;
        psi.UseShellExecute = false;
        psi.ErrorDialog = true;
    }
}

如何完成此测试?

我想编写一个单元测试用例来检查该过程是否会启动。

I am having a Java batch process which I used in C# to run process. I want to have a testcase to check whether the batch process is running or not.

I used the batch process as:

void batch_process(string process)
{
    try
    {
        string strFilePath = Path.Combine(batch_process_path, process);
        ProcessStartInfo psi = new ProcessStartInfo(strFilePath);
        psi.RedirectStandardOutput = true;
        psi.RedirectStandardError = true;
        psi.WorkingDirectory = batch_process_path;
        psi.WindowStyle = ProcessWindowStyle.Hidden;
        psi.UseShellExecute = false;
        psi.ErrorDialog = true;
    }
}

How can this test be done?

I want to write an unit test case to check whether the process will start or not.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

滿滿的愛 2024-11-14 14:59:09

当您启动新进程时,您需要捕获返回的Process,它提供对新启动进程的访问,对于启动、停止、控制和监视应用程序很有用。

 Process exe = Process.Start(psi);
    ....

 if exe.HasExited() ....

when you Start your new process you need to capture the returned Process, that provides access to newly started process, useful for starting, stopping, controlling, and monitoring applications.

 Process exe = Process.Start(psi);
    ....

 if exe.HasExited() ....
套路撩心 2024-11-14 14:59:09

您可以使用启动进程

Process process = new Process();
string strFilePath = Path.Combine(batch_process_path, process);
process.StartInfo.FileName = strFilePath;

//this line will hold this thread until the process is done.
process.WaitForExit();

,然后在不同的线程上启动进程,并让该线程在 process.WaitForExit(); 完成后触发事件。

You could start the process using

Process process = new Process();
string strFilePath = Path.Combine(batch_process_path, process);
process.StartInfo.FileName = strFilePath;

//this line will hold this thread until the process is done.
process.WaitForExit();

then start the process on a different thread and let that thread fire an event after process.WaitForExit(); is done.

等数载,海棠开 2024-11-14 14:59:09

您应该首先使用刚刚创建的 ProcessStartInfo 启动进程,如下所示:

Process process = Process.Start(psi);

然后您可以使用 process.HasExited 检查进程是否已退出。通常,您不需要这样做,因为 process.WaitForExit() 会阻止代码直到进程退出。

You should first start the process using the ProcessStartInfo you've just created like:

Process process = Process.Start(psi);

then you can use process.HasExited to check if the process has exited. Often, you don't need to do this, as process.WaitForExit() blocks the code until process exits.

錯遇了你 2024-11-14 14:59:09

我有点不确定你的问题中的场景...但是你可以使用的 4 种技术是:

一般来说...我更喜欢使用 Process.Exited,但其他方法也都有自己的位置!

I'm a bit unsure of the scenario from your question... but 4 techniques you can use are:

In general... I'd prefer to use Process.Exited, but the other methods all have their place too!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文