C#中進程的掛起與恢複的代碼執行個體分析

來源:互聯網
上載者:User
這篇文章主要介紹了C#中進程的掛起與恢複操作方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下

1. 源起:

仍然是模組化編程所引發的需求。產品經理難伺候,女產品經理更甚之~:p

純屬戲謔,技術方案與產品經理無關,芋頭莫怪!

VCU10項目重構,要求各功能模組以獨立進程方式實現,比如:音視頻轉換模組,若以獨立進程方式實現,如何控制其暫停、繼續等功能呢?

線程可以Suspend、Resume,c#內建的Process沒有此類方法,咋整?

山窮水盡疑無路,柳暗花明又一村。情到濃時清轉薄,此情可待成追憶!

前篇描述了進程間資料傳遞方法,此篇亦以樣本示範其間控制與資料互動方法。

2、未公開的API函數:NtSuspendProcess、NtResumeProcess

此類函數在MSDN中找不到。

思其原因,概因它們介於Windows API和 核心API之間,威力不容小覷。怕二八耙子程式員濫用而引發事端,因此密藏。

其實還有個NtTerminateProcess,因Process有Kill方法,因此可不用。

但再隱秘的東西,只要有價值,都會被人給翻出來,好酒不怕巷子深麼!

好,基於其,設計一個進程管理類,實現模組化編程之進程間控制這個需求。

3、ProcessMgr

直上代碼吧,封裝一個進程嵌入式管理單元:

public static class ProcessMgr {  /// <summary>  /// The process-specific access rights.  /// </summary>  [Flags]  public enum ProcessAccess : uint  {   /// <summary>   /// Required to terminate a process using TerminateProcess.   /// </summary>   Terminate = 0x1,   /// <summary>   /// Required to create a thread.   /// </summary>   CreateThread = 0x2,   /// <summary>   /// Undocumented.   /// </summary>   SetSessionId = 0x4,   /// <summary>   /// Required to perform an operation on the address space of a process (see VirtualProtectEx and WriteProcessMemory).   /// </summary>   VmOperation = 0x8,   /// <summary>   /// Required to read memory in a process using ReadProcessMemory.   /// </summary>   VmRead = 0x10,   /// <summary>   /// Required to write to memory in a process using WriteProcessMemory.   /// </summary>   VmWrite = 0x20,   /// <summary>   /// Required to duplicate a handle using DuplicateHandle.   /// </summary>   DupHandle = 0x40,   /// <summary>   /// Required to create a process.   /// </summary>   CreateProcess = 0x80,   /// <summary>   /// Required to set memory limits using SetProcessWorkingSetSize.   /// </summary>   SetQuota = 0x100,   /// <summary>   /// Required to set certain information about a process, such as its priority class (see SetPriorityClass).   /// </summary>   SetInformation = 0x200,   /// <summary>   /// Required to retrieve certain information about a process, such as its token, exit code, and    priority class (see OpenProcessToken, GetExitCodeProcess, GetPriorityClass, and IsProcessInJob).   /// </summary>   QueryInformation = 0x400,   /// <summary>   /// Undocumented.   /// </summary>   SetPort = 0x800,   /// <summary>   /// Required to suspend or resume a process.   /// </summary>   SuspendResume = 0x800,   /// <summary>   /// Required to retrieve certain information about a process (see QueryFullProcessImageName).    A handle that has the PROCESS_QUERY_INFORMATION access right is automatically granted PROCESS_QUERY_LIMITED_INFORMATION.   /// </summary>   QueryLimitedInformation = 0x1000,   /// <summary>   /// Required to wait for the process to terminate using the wait functions.   /// </summary>   Synchronize = 0x100000  }  [DllImport("ntdll.dll")]  private static extern uint NtResumeProcess([In] IntPtr processHandle);  [DllImport("ntdll.dll")]  private static extern uint NtSuspendProcess([In] IntPtr processHandle);  [DllImport("kernel32.dll", SetLastError = true)]  private static extern IntPtr OpenProcess(   ProcessAccess desiredAccess,   bool inheritHandle,   int processId);  [DllImport("kernel32.dll", SetLastError = true)]  [return: MarshalAs(UnmanagedType.Bool)]  private static extern bool CloseHandle([In] IntPtr handle);  public static void SuspendProcess(int processId)  {   IntPtr hProc = IntPtr.Zero;   try   {    // Gets the handle to the Process    hProc = OpenProcess(ProcessAccess.SuspendResume, false, processId);    if (hProc != IntPtr.Zero)     NtSuspendProcess(hProc);   }   finally   {    // Don't forget to close handle you created.    if (hProc != IntPtr.Zero)     CloseHandle(hProc);   }  }  public static void ResumeProcess(int processId)  {   IntPtr hProc = IntPtr.Zero;   try   {    // Gets the handle to the Process    hProc = OpenProcess(ProcessAccess.SuspendResume, false, processId);    if (hProc != IntPtr.Zero)     NtResumeProcess(hProc);   }   finally   {    // Don't forget to close handle you created.    if (hProc != IntPtr.Zero)     CloseHandle(hProc);   }  } }

4、進程式控制制

我權且主進程為宿主,它通過Process類調用子進程,得其ID,以此為用。其調用代碼為:

 private void RunTestProcess(bool hidden = false)  {   string appPath = Path.GetDirectoryName(Application.ExecutablePath);   string testAppPath = Path.Combine(appPath, "TestApp.exe");   var pi = new ProcessStartInfo();   pi.FileName = testAppPath;   pi.Arguments = this.Handle.ToString();   pi.WindowStyle = hidden ? ProcessWindowStyle.Hidden : ProcessWindowStyle.Normal;   this.childProcess = Process.Start(pi);   txtInfo.Text = string.Format("子進程ID:{0}\r\n子進程名:{1}", childProcess.Id, childProcess.ProcessName);   ...  }

控制碼為:

private void btnWork_Click(object sender, EventArgs e)  {   if (this.childProcess == null || this.childProcess.HasExited)    return;   if ((int)btnWork.Tag == 0)   {    btnWork.Tag = 1;    btnWork.Text = "恢複";    ProcessMgr.SuspendProcess(this.childProcess.Id);   }   else   {    btnWork.Tag = 0;    btnWork.Text = "掛起";    ProcessMgr.ResumeProcess(this.childProcess.Id);   }  }

子進程以一定時器類比其工作,向主進程拋進度訊息:

 private void timer_Tick(object sender, EventArgs e)  {   if (progressBar.Value < progressBar.Maximum)    progressBar.Value += 1;   else    progressBar.Value = 0;   if (this.hostHandle != IntPtr.Zero)    SendMessage(this.hostHandle, WM_PROGRESS, 0, progressBar.Value);  }

代碼量就這麼的少,簡單吧……

5、:

為樣本,做了兩個圖,其一為顯示子進程,其二為隱藏子進程。

實際項目調用獨立進程模組,是以隱藏方式調用的,以宿主展示其處理進度,如此圖:

後記:

擴充思路,一些優秀的開源工具,如youtube_dl、ffmpeg等,都以獨立進程方式存在,且可通過CMD管理通訊。

以此進程式控制制原理,可以基於這些開源工具,做出相當不錯的GUI工具出來。畢竟相對於強大的命令列,人們還是以簡單操作為方便。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.