2014-02-17 113 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

不按預期工作的原因是因爲你進入循環,啓動程序並等待程序結束後再開始下一個循環。您應該將循環中的進程監視移到另一個Thread上,以便所有程序都可以立即啓動。 – 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

的問題是在內部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); 

      } 
     } 
    } 
} 
+0

謝謝你,但我想知道,例如當我要關閉我已經啓動的程序「峯值物理內存使用的過程」。我也希望它每2秒鐘刷新一次RAM使用情況的信息,就像我編寫程序一樣。我希望我的程序能夠提供相同的信息,但能夠同時運行多個程序。 –

+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

謝謝,迄今爲止它工作的很棒。我相信這就是我一直在尋找的! –