3

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

        }
4
  • I want to run code for scenarios like - 5 thread execute 1 application currently i'm doing one thread run method 5 times. Commented May 29, 2018 at 10:52
  • So your problem is nothing to do with threads. You just want to know how to execute a console app from a .NET program? Commented May 29, 2018 at 13:09
  • Possible duplicate of How to execute command in a C# Windows console app? Commented May 29, 2018 at 13:39
  • i want to use multiple thread also while executing c# I tried like this - updated question,, Commented May 29, 2018 at 13:40

1 Answer 1

4

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.
}
Sign up to request clarification or add additional context in comments.

2 Comments

instead of executing method I want to call console app using that thread , how to do it?
I updated my answer. Now it creates 5 threads, which create 5 processes and opens the console app 5 time. But in this case i think a thread is not needed. You could directly create 5 processes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.