C# 中的批处理
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当您
启动
新进程时,您需要捕获返回的Process
,它提供对新启动进程的访问,对于启动、停止、控制和监视应用程序很有用。when you
Start
your new process you need to capture the returnedProcess
, that provides access to newly started process, useful for starting, stopping, controlling, and monitoring applications.您可以使用启动进程
,然后在不同的线程上启动进程,并让该线程在 process.WaitForExit(); 完成后触发事件。
You could start the process using
then start the process on a different thread and let that thread fire an event after
process.WaitForExit();
is done.您应该首先使用刚刚创建的
ProcessStartInfo
启动进程,如下所示:然后您可以使用
process.HasExited
检查进程是否已退出。通常,您不需要这样做,因为 process.WaitForExit() 会阻止代码直到进程退出。You should first start the process using the
ProcessStartInfo
you've just created like: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.我有点不确定你的问题中的场景...但是你可以使用的 4 种技术是:
如果你已经使用
var process = Process.Start(psi); 启动了该过程
那么:process.HasExited
- http://msdn.microsoft.com/en-us/library/system.diagnostics.process.hasexited.aspxProcess.Exited
- http://msdn.microsoft.com/en-us/library/system.diagnostics.process.exited.aspxProcess.WaitForExit
- http://msdn.microsoft.com/en-us/library/system.diagnostics.process.waitforexit.aspx如果您的进程以其他方式启动并且具有一些唯一的名称,那么您可以检查返回的枚举通过 System.Diagnostics.Process.GetProcesses() 来确定您的批处理进程当前是否正在运行。
一般来说...我更喜欢使用 Process.Exited,但其他方法也都有自己的位置!
I'm a bit unsure of the scenario from your question... but 4 techniques you can use are:
if you have started the process using
var process = Process.Start(psi);
then:process.HasExited
- http://msdn.microsoft.com/en-us/library/system.diagnostics.process.hasexited.aspxProcess.Exited
- http://msdn.microsoft.com/en-us/library/system.diagnostics.process.exited.aspxProcess.WaitForExit
- http://msdn.microsoft.com/en-us/library/system.diagnostics.process.waitforexit.aspxif your process is started in some other way and has some unique name, then you can inspect the enumeration returned by
System.Diagnostics.Process.GetProcesses()
to determine if your batch process is currently running.In general... I'd prefer to use
Process.Exited
, but the other methods all have their place too!