相同的exe文件、多进程和不同的输入参数

本文关键字:输入 参数 exe 文件 多进程 | 更新日期: 2023-09-27 18:26:53

我有一个exe文件,比如XYZ.exe,它接受一个csv文件,并进行一些其他操作,比如根据csv文件中的内容查询DB。

现在,有4个csv文件,file_1.csv到file_4.csv,格式相同,但内容不同。我想做的是初始化4个进程,所有进程都运行XYZ.exe,每个进程都有一个csv文件。它们都在后台运行。

我尝试过使用Process.Start(@"XYZ.exe",输入参数)。然而,第二个过程似乎要等到第一个过程结束后才能开始。我想知道我应该如何更改代码才能完成这项工作。

相同的exe文件、多进程和不同的输入参数

使用此重载:
Process.Start方法(ProcessStartInfo)

@JimMischel你应该得到积分奖励!

我认为你在这里尝试做的叫做:多线程,我的想法是你可以创建一个启动4个线程的应用程序,然后一起启动所有线程,它是这样的:

using System.Threading;

{
    class Program
    {
        static void Main(string[] args)
        {
            //Here is your main program :
            //Initiate the thread (tell it what to do)
            ThreadStart testThreadStart = new ThreadStart(new Program().testThread);
            //Create 4 Threads and give the Path of the 4 CSV files to process
            Thread Thread1 = new Thread(() => testThreadStart(PathOfThe1stCSVFile));
            Thread Thread2 = new Thread(() => testThreadStart(PathOfThe2ndCSVFile));
            Thread Thread3 = new Thread(() => testThreadStart(PathOfThe3rdCSVFile));
            Thread Thread4 = new Thread(() => testThreadStart(PathOfThe4thCSVFile));
            //Start All The Threads Together
            testThread1.Start();
            testThread2.Start();
            testThread3.Start();
            testThread4.Start();
            //All The Threads  are finished
            Console.ReadLine();
        }

        public void testThread(String CSVfilePathToProcess)
        {
            //executing in thread
            //put the : Process.Start(@"XYZ.exe", input arguments). here !!!
            //and put the CSVfilePathToProcess as arguments
        }
    }
}

编辑:如果多线程对你来说有点复杂,你也可以在C#中使用backgroundworker,但想法是一样的。