国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

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

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
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 欧美在线视频日韩 | 久久国产精品一区二区 | 欧美视频二区 | 成人精品动漫一区二区三区 | 日韩欧美一区二区三区免费观看 | 亚洲精品一二区 | 日日操综合 | 欧美一级精品 | 色在线免费| 色视频免费在线观看 | 国产一区二区在线免费观看 | 久久亚洲一区二区 | 91久久极品 | 中文字幕av网站 | 高清国产午夜精品久久久久久 | 国产精品一区二区三区四区 | 日韩欧美精品在线 | 免费黄网视频 | 亚洲最新无码中文字幕久久 | 黄片毛片毛片毛片 | 成人在线免费 | 午夜精品美女久久久久av福利 | 中文字幕国产视频 | 国产97在线播放 | 精品一区二区三区免费视频 | 精品国产青草久久久久福利 | 夜夜爽99久久国产综合精品女不卡 | 激情欧美一区二区三区中文字幕 | 久久精品视频一区 | 夜夜av| 国产一区www | 黄久久久| 国产一区二区三区在线观看视频 | 开心久久婷婷综合中文字幕 | 精品日韩一区二区三区 | 免费毛片一级 | 亚洲国产精品一区在线 | 免费观看a毛片 | 日韩高清国产一区在线 | 久久精品国产91精品亚洲高清 | 精品久久久久久国产 |