작은 프로그램 (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
: "경우 (! myProcess.HasExited) "및"while (! myProcess.WaitForExit (1000)); " –
예상대로 작동하지 않는 이유는 루프를 입력하고 프로그램을 시작한 후 다음 루프를 시작하기 전에 프로그램이 끝날 때까지 기다리기 때문입니다. 모든 프로그램이 즉시 시작될 수 있도록 프로세스 모니터링을 루프 바깥으로 이동하고 가능하면 다른 스레드로 이동해야합니다. – Nunners