一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - C# - C#中進程的掛起與恢復

C#中進程的掛起與恢復

2021-12-30 14:13楚人無衣 C#

這篇文章主要介紹了C#中進程的掛起與恢復操作方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下

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、效果圖:

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

C#中進程的掛起與恢復

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

C#中進程的掛起與恢復

后記:

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

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

原文鏈接:http://www.cnblogs.com/crwy/p/6622319.html

延伸 · 閱讀

精彩推薦
  • C#Unity3D實現虛擬按鈕控制人物移動效果

    Unity3D實現虛擬按鈕控制人物移動效果

    這篇文章主要為大家詳細介紹了Unity3D實現虛擬按鈕控制人物移動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一...

    shenqingyu060520232410972022-03-11
  • C#WPF 自定義雷達圖開發實例教程

    WPF 自定義雷達圖開發實例教程

    這篇文章主要介紹了WPF 自定義雷達圖開發實例教程,本文介紹的非常詳細,具有參考借鑒價值,需要的朋友可以參考下...

    WinterFish13112021-12-06
  • C#C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題實例

    C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題實例

    這篇文章主要介紹了C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題,簡單描述了訪問者模式的定義并結合具體實例形式分析了C#使用訪問者模式解決長...

    GhostRider9502022-01-21
  • C#C#實現XML文件讀取

    C#實現XML文件讀取

    這篇文章主要為大家詳細介紹了C#實現XML文件讀取的相關代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    Just_for_Myself6702022-02-22
  • C#C# 實現對PPT文檔加密、解密及重置密碼的操作方法

    C# 實現對PPT文檔加密、解密及重置密碼的操作方法

    這篇文章主要介紹了C# 實現對PPT文檔加密、解密及重置密碼的操作方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下...

    E-iceblue5012022-02-12
  • C#深入解析C#中的交錯數組與隱式類型的數組

    深入解析C#中的交錯數組與隱式類型的數組

    這篇文章主要介紹了深入解析C#中的交錯數組與隱式類型的數組,隱式類型的數組通常與匿名類型以及對象初始值設定項和集合初始值設定項一起使用,需要的...

    C#教程網6172021-11-09
  • C#C#通過KD樹進行距離最近點的查找

    C#通過KD樹進行距離最近點的查找

    這篇文章主要為大家詳細介紹了C#通過KD樹進行距離最近點的查找,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    帆帆帆6112022-01-22
  • C#C#裁剪,縮放,清晰度,水印處理操作示例

    C#裁剪,縮放,清晰度,水印處理操作示例

    這篇文章主要為大家詳細介紹了C#裁剪,縮放,清晰度,水印處理操作示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    吳 劍8332021-12-08
主站蜘蛛池模板: 满城尽带黄金甲大胸片 | 精品国产免费久久久久久婷婷 | 日韩高清成人毛片不卡 | 国产成人精品一区二区不卡 | 久久99re2在线视频精品 | 国产精品久久亚洲一区二区 | 热色综合 | 色婷婷久久综合中文久久一本 | 国产免费资源高清小视频在线观看 | 四虎com| 爱情岛论坛自拍永久入口 | 亚洲成人综合在线 | 日日日操 | 免费看日产一区二区三区 | 日本亚洲欧洲高清有码在线播放 | 韩国靠逼| 日本无遮挡亲吻膜下面免费 | 亚洲系列国产系列 | 嗯啊好大视频 | 国外欧美一区另类中文字幕 | 久久视频这有精品63在线国产 | bt伙计最新合集 | 欧美亚洲综合另类 | 青青青视频蜜桃一区二区 | 精品国产日韩亚洲一区在线 | 青青草原免费在线视频 | 国内外精品免费视频 | 午夜一级毛片看看 | 欧美一级专区免费大片俄罗斯 | 鄂州一家三口完整版免费 | 26uuu久久 | 91高清国产经典在线观看 | 国产在线精品亚洲第一区香蕉 | 国产欧美一区二区成人影院 | 日本又黄又裸一级大黄裸片 | 男女爆操 | 日本草草视频 | 美女下面揉出水免费视频 | 色香视频在线 | 激情小说色图 | 99精品国产高清自在线看超 |