从另一个C#程序调用C#程序

时间:2014-12-16 13:19:05

标签: c#

我有3个C#程序需要按顺序执行(TestCSharp1,TestCSharp2和TestCSharp3).2nd程序应该只在第一次完成后执行,3只在1和2完成后执行。我怎样才能做到这一点。现在,我将它们作为计划任务,我手动检查它们是否已完成,然后启动其他任务。

2 个答案:

答案 0 :(得分:3)

使用Proccess班级(Documentation)从您的计划中启动流程。以下是文档中的示例:

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);
            }
        }
    }
}

答案 1 :(得分:2)

假设您正在运行命令行程序,您可以创建一个包含三个可执行文件的批处理文件,并将批处理文件作为计划任务运行。正如您在评论中看到的那样,如果有Windows程序,这种方法将无效。

E.g。

@echo off 
cd \ToTheRightPlace
TestCSharp1
TestCSharp2
TestCSharp3

您想要查看返回值等