Neo
Neo

Reputation: 16219

How to execute console app using multiple threads c#

I want to execute application using multiple threads using c#

I tried like below for normal method what about app execution ?

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

        }

Upvotes: 3

Views: 7232

Answers (1)

Jurgis Gleixner
Jurgis Gleixner

Reputation: 96

I don't know if I understood the question correctly. This code has n threads which execute the same method

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.
}

Upvotes: 4

Related Questions