Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

        }
like image 283
Neo Avatar asked Jun 21 '26 12:06

Neo


1 Answers

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.
}
like image 83
Jurgis Gleixner Avatar answered Jun 24 '26 00:06

Jurgis Gleixner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!