C# 一次启动几个程序

C# 一次启动几个程序,c#,C#,我编写了一个小程序(ProcessSample)来启动另一个在.txt文件中定义的程序(用新行分隔) 代码主要取自MSDN。我刚刚开始我的编程冒险,但我想写一些有用的东西 我不知道从ProcessSample程序中同时运行两个程序的明智方法 在我的.txt文件中,我只有指向带有.exe的程序的路径。一切正常,但我的程序一次只运行一个程序。我想我会运行foreach,但它当然不会在这里工作,因为它只运行第一个程序,它会等到我退出它,然后它会运行下一个程序 所以我知道它不起作用的原因。我只是想知道我

我编写了一个小程序(ProcessSample)来启动另一个在.txt文件中定义的程序(用新行分隔)

代码主要取自MSDN。我刚刚开始我的编程冒险,但我想写一些有用的东西

我不知道从ProcessSample程序中同时运行两个程序的明智方法

在我的.txt文件中,我只有指向带有.exe的程序的路径。一切正常,但我的程序一次只运行一个程序。我想我会运行foreach,但它当然不会在这里工作,因为它只运行第一个程序,它会等到我退出它,然后它会运行下一个程序

所以我知道它不起作用的原因。我只是想知道我怎样才能让它按我想要的方式工作

我的C#代码:


问题出在内部while循环中。在这里,您可以从正在运行的进程中获取统计信息,并在控制台中显示它们。据我从您的帖子中了解,您不需要此功能,因此可以删除它,您将获得:

using System;
using System.Diagnostics;
using System.Threading;

namespace ProcessSample
{
    class ProcessMonitorSample
    {
        public static void Main()
        {
            Console.BufferHeight = 25;

            // Define variables to track the peak memory usage of the process. 
            long peakWorkingSet = 0;

            string[] Programs = System.IO.File.ReadAllLines(@"C:\Programs\list.txt");

            foreach (string Program in Programs)
            {
                Process myProcess = null;

                // Start the process.
                myProcess = Process.Start(@Program);

                Console.WriteLine("Program started: {0}", Program);

            }
        }
    }
}

您可以用Parallel.foreach替换您的foreach以实现您想要的

 Parallel.ForEach<String>(Programs, Program =>
            {
                Process myProcess = null;

                // Start the process.
                myProcess = Process.Start(@Program);

                // Display the process statistics until 
                // the user closes the program. 
                do
                {
                    if (!myProcess.HasExited)
                    {
                        // Refresh the current process property values.
                        myProcess.Refresh();

                        // Display current process statistics.

                        Console.WriteLine();
                        Console.WriteLine("Path: {0}, RAM: {1}", Program, (myProcess.WorkingSet64 / 1024 / 1024));

                        // Update the values for the overall peak memory statistics.
                        peakWorkingSet = myProcess.PeakWorkingSet64;

                        if (myProcess.Responding)
                        {
                            Console.WriteLine("Status: Running");
                        }
                        else
                        {
                            Console.WriteLine("Status: Not Responding!");
                        }

                        // Wait 2 seconds
                        Thread.Sleep(2000);
                    }
                }
                while (!myProcess.WaitForExit(1000));

                Console.WriteLine();
                Console.WriteLine("Process exit code: {0}", myProcess.ExitCode);
                Console.WriteLine("Peak physical memory usage of the process: {0}", (peakWorkingSet / 1024 / 1024));

                Console.WriteLine("Press any key to exit.");
                System.Console.ReadKey();
            });
Parallel.ForEach(程序,程序=>
{
进程myProcess=null;
//开始这个过程。
myProcess=Process.Start(@Program);
//显示进程统计信息,直到
//用户关闭程序。
做
{
如果(!myProcess.hasExit)
{
//刷新当前进程属性值。
myProcess.Refresh();
//显示当前进程统计信息。
Console.WriteLine();
WriteLine(“路径:{0},内存:{1}”,程序,(myProcess.WorkingSet64/1024/1024));
//更新总峰值内存统计信息的值。
peakWorkingSet=myProcess.PeakWorkingSet64;
if(myProcess.Responding)
{
Console.WriteLine(“状态:正在运行”);
}
其他的
{
Console.WriteLine(“状态:无响应!”);
}
//等2秒钟
《睡眠》(2000年);
}
}
而(!myProcess.WaitForExit(1000));
Console.WriteLine();
WriteLine(“进程退出代码:{0}”,myProcess.ExitCode);
WriteLine(“进程的物理内存使用峰值:{0}”,(peakWorkingSet/1024/1024));
控制台。WriteLine(“按任意键退出”);
System.Console.ReadKey();
});

事实上,我已经了解了线程,并在我的程序中使用了线程,如下所示:

class Program
{
static void Main(string[] args)
{
    string[] myApps = { "notepad.exe", "calc.exe", "explorer.exe" };

    Thread w;
    ParameterizedThreadStart ts = new ParameterizedThreadStart(StartMyApp);
    foreach (var myApp in myApps)
    {
        w = new Thread(ts);
        w.Start(myApp);
        Thread.Sleep(1000);
    }
}

private static void StartMyApp(object myAppPath)
{
    ProcessStartInfo myInfoProcess = new ProcessStartInfo();
    myInfoProcess.FileName = myAppPath.ToString();
    myInfoProcess.WindowStyle = ProcessWindowStyle.Minimized;
    Process myProcess = Process.Start(myInfoProcess);

    do
    {
        if (!myProcess.HasExited)
        {
            myProcess.Refresh(); // Refresh the current process property values.
            Console.WriteLine(myProcess.ProcessName+" RAM: "+(myProcess.WorkingSet64 / 1024 / 1024).ToString()+"\n");
            Thread.Sleep(1000);
        }
    }
    while (!myProcess.WaitForExit(1000)); 
}

}

您正在显式检查程序是否已完成执行:“if(!myProcess.HasExited)”和“while(!myProcess.WaitForExit(1000));”它不能按预期工作的原因是,您进入循环,启动程序并等待程序结束,然后再启动下一个循环。您应该将进程监视移到循环之外,并可能移到另一个线程上,以便可以同时启动所有程序。谢谢您,但我想知道,例如,当我要关闭已启动的程序时,“进程的物理内存使用峰值”。我还希望它每2秒刷新一次RAM使用信息,就像我写程序一样。我希望我的程序能够提供相同的信息,但能够同时运行多个程序。好的,最简单的方法是用另一个答案中描述的并行方法替换for-each。谢谢,到目前为止,它工作得非常好。我相信这就是我要找的!
class Program
{
static void Main(string[] args)
{
    string[] myApps = { "notepad.exe", "calc.exe", "explorer.exe" };

    Thread w;
    ParameterizedThreadStart ts = new ParameterizedThreadStart(StartMyApp);
    foreach (var myApp in myApps)
    {
        w = new Thread(ts);
        w.Start(myApp);
        Thread.Sleep(1000);
    }
}

private static void StartMyApp(object myAppPath)
{
    ProcessStartInfo myInfoProcess = new ProcessStartInfo();
    myInfoProcess.FileName = myAppPath.ToString();
    myInfoProcess.WindowStyle = ProcessWindowStyle.Minimized;
    Process myProcess = Process.Start(myInfoProcess);

    do
    {
        if (!myProcess.HasExited)
        {
            myProcess.Refresh(); // Refresh the current process property values.
            Console.WriteLine(myProcess.ProcessName+" RAM: "+(myProcess.WorkingSet64 / 1024 / 1024).ToString()+"\n");
            Thread.Sleep(1000);
        }
    }
    while (!myProcess.WaitForExit(1000)); 
}