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东西出来。毕竟相对于壮大的命令行,人们还是以简朴操作为轻易。
以上就是C#中历程的挂起与恢复的代码实例剖析(图)的细致内容,更多请关注ki4网别的相干文章!