2014-02-17 3 views
1

작은 프로그램 (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

: "경우 (! myProcess.HasExited) "및"while (! myProcess.WaitForExit (1000)); " –

+0

예상대로 작동하지 않는 이유는 루프를 입력하고 프로그램을 시작한 후 다음 루프를 시작하기 전에 프로그램이 끝날 때까지 기다리기 때문입니다. 모든 프로그램이 즉시 시작될 수 있도록 프로세스 모니터링을 루프 바깥으로 이동하고 가능하면 다른 스레드로 이동해야합니다. – Nunners

답변

0

는 사실은 내가 스레드에 대해 배운 내가 이렇게 내 프로그램을 사용하고 있습니다 :

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

}

프로그램의 실행 완료 경우 명시 적으로 확인하고
1

문제는 루프 동안 내부에있다. 실행중인 프로세스에서 통계를 가져와 콘솔에 표시합니다.

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

      } 
     } 
    } 
} 
+0

고마워요.하지만 제가 시작한 프로그램을 닫을 때 예를 들어 "프로세스의 최대 실제 메모리 사용량"을 알고 싶습니다. 또한 프로그램을 작성한 것과 같은 방식으로 RAM 사용에 대한 정보로 매 2 초마다 새로 고치기를 원합니다. 내 프로그램이 동일한 정보를 제공하지만 동시에 많은 프로그램을 동시에 실행할 수있는 기능을 원합니다. –

+0

좋아, 가장 쉬운 방법은 각각에 대해 다른 답변 – peter

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(); 
      }); 
+0

에 설명 된대로 각각에 대한 병렬로 대체하는 것입니다 감사합니다, 그것은 지금까지 굉장한. 나는 그것이 내가 찾고 있었던 것이라고 믿는다! –