1. 源起:
仍然是模塊化編程所引發的需求。產品經理難伺候,女產品經理更甚之~:p
純屬戲謔,技術方案與產品經理無關,芋頭莫怪!
vcu10項目重構,要求各功能模塊以獨立進程方式實現,比如:音視頻轉換模塊,若以獨立進程方式實現,如何控制其暫停、繼續等功能呢?
線程可以suspend、resume,c#內置的process沒有此類方法,咋整?
山窮水盡疑無路,柳暗花明又一村。情到濃時清轉薄,此情可待成追憶!
前篇描述了進程間數據傳遞方法,此篇亦以示例演示其間控制與數據交互方法。
2、未公開的api函數:ntsuspendprocess、ntresumeprocess
此類函數在msdn中找不到。
思其原因,概因它們介于windows api和 內核api之間,威力不容小覷。怕二八耙子程序員濫用而引發事端,因此密藏。
其實還有個ntterminateprocess,因process有kill方法,因此可不用。
但再隱秘的東西,只要有價值,都會被人給翻出來,好酒不怕巷子深么!
好,基于其,設計一個進程管理類,實現模塊化編程之進程間控制這個需求。
3、processmgr
直上代碼吧,封裝一個進程管理單元:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
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,以此為用。其調用代碼為:
1
2
3
4
5
6
7
8
9
10
11
12
|
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); ... } |
控制代碼為:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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); } } |
子進程以一定時器模擬其工作,向主進程拋進度消息:
1
2
3
4
5
6
7
8
9
|
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工具出來。畢竟相對于強大的命令行,人們還是以簡單操作為方便。
原文鏈接:http://www.cnblogs.com/crwy/p/6622319.html