【问题标题】:How to execute console app using multiple threads c#如何使用多线程c#执行控制台应用程序
【发布时间】:2018-05-29 10:42:26
【问题描述】:

我想使用 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);

        }

【问题讨论】:

  • 我想为以下场景运行代码 - 5 thread execute 1 application 目前我正在执行一个线程运行方法 5 次。
  • 所以你的问题与线程无关。您只想知道如何从 .NET 程序执行控制台应用程序?
  • 我想在执行 c# 时也使用多线程我试过这样 - 更新的问题,,

标签: c# multithreading thread-safety


【解决方案1】:

我不知道我是否正确理解了这个问题。这段代码有 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.
}

【讨论】:

  • 我想使用该线程调用控制台应用程序,而不是执行方法,该怎么做?
  • 我更新了我的答案。现在它创建了 5 个线程,这些线程创建了 5 个进程并打开了控制台应用程序 5 次。但在这种情况下,我认为不需要线程。你可以直接创建5个进程
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-24
  • 1970-01-01
相关资源
最近更新 更多