从另一个 C# 程序调用 C# 程序
本文关键字:程序 调用 另一个 | 更新日期: 2023-09-27 18:34:19
我有 3 个 C# 程序需要按顺序执行(TestCSharp1、TestCSharp2 和 TestCSharp3).第 2 个程序应仅在第一次完成后执行,3 个程序应仅在 1 和 2 完成后执行。我该怎么做。现在,我将它们作为计划任务,我手动检查它们是否已完成,然后启动其他任务。
使用 Proccess
类(文档)从程序内部启动进程。以下是文档中的示例:
using System;
using System.Diagnostics;
using System.ComponentModel;
namespace MyProcessSample
{
class MyProcess
{
public static void Main()
{
Process myProcess = new Process();
try
{
myProcess.StartInfo.UseShellExecute = false;
// You can start any process, HelloWorld is a do-nothing example.
myProcess.StartInfo.FileName = "C:''HelloWorld.exe";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
myProcess.WaitForExit(); //use this if you want to pause execution of your program till the process you have started closes.
// This code assumes the process you are starting will terminate itself.
// Given that is is started without a window so you cannot terminate it
// on the desktop, it must terminate itself or you can do it programmatically
// from this application using the Kill method.
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
假设您正在运行命令行程序,则可以创建一个包含三个可执行文件的批处理文件,并将批处理文件作为计划任务运行。正如您将在评论中看到的那样,如果有 Windows 程序,这种方法将不起作用。
例如
@echo off
cd 'ToTheRightPlace
TestCSharp1
TestCSharp2
TestCSharp3
您需要检查返回值等。