簡體   English   中英

如何使用多個線程執行控制台應用程序C#

[英]How to execute console app using multiple threads c#

我想使用C#使用多個線程執行應用程序

我嘗試了下面的正常方法,應用執行如何?

 public static void OneThread()
        {
            DateTime startTime = DateTime.Now;

            Thread t11 = new Thread(() =>
            {
                for (int i = 0; i <= 5; i++)
                {
                    var proc = new Process();
        proc.StartInfo.FileName = @"C:\Users\consoleapp.exe";
        proc.StartInfo.Arguments = "-v -s -a";
        proc.Start();
        proc.WaitForExit();
        var exitCode = proc.ExitCode;
        proc.Close();
                }
            });

            t11.Start();
            t11.Join();
            Console.WriteLine("execution 1 thread 5 times in {0} seconds", (DateTime.Now - startTime).TotalSeconds);

        }

我不知道我是否正確理解了這個問題。 此代碼具有執行相同方法的n個線程

int n = 5;
for (int i = 0; i < n; i++)
{
    Thread t = new Thread(MethodToExecute);
    t.Start();
}


public void MethodToExecute()
{
    Process process = new Process();
    // Configure the process using the StartInfo properties.
    process.StartInfo.FileName = "pathToConsoleApp.exe";
    process.Start();
    process.WaitForExit();// Waits here for the process to exit.
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM