我编写了一个小程序(ProcessSample)来启动.txt文件中定义的另一个程序(用新行分隔)。
代码主要来自MSDN。我刚开始编程冒险,但我想写一些有用的东西。
我不知道从我的ProcessSample程序同时运行两个程序的智能方法。
在我的.txt文件中,我只有使用.exe的程序路径。一切正常,但我的程序当时只运行一个程序。我以为我会运行foreach但当然它不会在这里工作,因为它只运行第一个程序并等待直到我退出它,然后它将运行下一个程序。
所以我知道它为什么不起作用的原因。我只是想知道如何让它按照我想要的方式工作。
我的C#代码:
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);
// 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);
} // if
} // do
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();
} // foreach
} // public
} //class
} // namespace
答案 0 :(得分:1)
问题出在内部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);
}
}
}
}
答案 1 :(得分:0)
您可以使用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();
});
答案 2 :(得分:0)
实际上我已经了解了Threads,我已经将它们用于我的程序:
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));
}
}