1.  
  2. namespace Test
  3. {
  4. using System;
  5. using System.Windows.Forms;
  6. static class Program
  7. {
  8. /// <summary>
  9. /// The main entry point for the application.
  10. /// </summary>
  11. [STAThread]
  12. static void Main()
  13. {
  14. Application.EnableVisualStyles();
  15. Application.SetCompatibleTextRenderingDefault(false);
  16. Application.Run(new MainForm());
  17. }
  18. }
  19. }
  20. namespace Test
  21. {
  22. using System;
  23. using System.Threading;
  24. using System.Windows.Forms;
  25. using Microshaoft;
  26. partial class MainForm
  27. {
  28. /// <summary>
  29. /// Required designer variable.
  30. /// </summary>
  31. private System.ComponentModel.IContainer components = null;
  32. /// <summary>
  33. /// Clean up any resources being used.
  34. /// </summary>
  35. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  36. protected override void Dispose(bool disposing)
  37. {
  38. if (disposing && (components != null))
  39. {
  40. components.Dispose();
  41. }
  42. base.Dispose(disposing);
  43. }
  44. #region Windows Form Designer generated code
  45. /// <summary>
  46. /// Required method for Designer support - do not modify
  47. /// the contents of this method with the code editor.
  48. /// </summary>
  49. private void InitializeComponent()
  50. {
  51. this.button1 = new System.Windows.Forms.Button();
  52. this.SuspendLayout();
  53. //
  54. // button1
  55. //
  56. this.button1.Location = new System.Drawing.Point(119, 74);
  57. this.button1.Name = "button1";
  58. this.button1.Size = new System.Drawing.Size(75, 23);
  59. this.button1.TabIndex = 0;
  60. this.button1.Text = "button1";
  61. this.button1.UseVisualStyleBackColor = true;
  62. this.button1.Click += new System.EventHandler(this.button1_Click);
  63. //
  64. // MainForm
  65. //
  66. this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
  67. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  68. this.ClientSize = new System.Drawing.Size(282, 253);
  69. this.Controls.Add(this.button1);
  70. this.Name = "MainForm";
  71. this.Text = "MainForm";
  72. this.ResumeLayout(false);
  73. }
  74. #endregion
  75. private System.Windows.Forms.Button button1;
  76. }
  77. public partial class MainForm : Form
  78. {
  79. public MainForm()
  80. {
  81. InitializeComponent();
  82. }
  83. private void button1_Click(object sender, EventArgs e)
  84. {
  85. var r = TaskProcesserHelper.ProcessWaitingShowDialog40
  86. (
  87. this
  88. , new ProcessWaitingCancelableDialog()
  89. , () =>
  90. {
  91. Thread.Sleep(5 * 1000);
  92. //throw new Exception();
  93. }
  94. , () =>
  95. {
  96. Console.WriteLine("Finished");
  97. }
  98. , (x) =>
  99. {
  100. Console.WriteLine("Caught Exception: {0}", x);
  101. }
  102. );
  103. Console.WriteLine(r);
  104. }
  105. }
  106. }
  107. namespace Microshaoft
  108. {
  109. using System;
  110. using System.Threading;
  111. using System.Threading.Tasks;
  112. using System.Windows.Forms;
  113. public static class TaskProcesserHelper
  114. {
  115. public static int ProcessWaitingShowDialog
  116. (
  117. IWin32Window ownerWindow
  118. , Func<Form> onWaitingDialogFactoryFunc
  119. , Action onProcessAction = null
  120. , Action onProcessedAction = null
  121. , Action<Exception> onCaughtExceptionProcessAction = null
  122. )
  123. {
  124. var dialogForm = onWaitingDialogFactoryFunc();
  125. return
  126. ProcessWaitingShowDialog
  127. (
  128. ownerWindow
  129. , dialogForm
  130. , onProcessAction = null
  131. , onProcessedAction = null
  132. , onCaughtExceptionProcessAction = null
  133. );
  134. }
  135. public static int ProcessWaitingShowDialog40
  136. (
  137. IWin32Window ownerWindow
  138. , Form dialogForm
  139. , Action onProcessAction = null
  140. , Action onProcessedAction = null
  141. , Action<Exception> onCaughtExceptionProcessAction = null
  142. )
  143. {
  144. var r = 1;
  145. Task<DialogResult> task1 = Task.Factory.StartNew<DialogResult>
  146. (
  147. () =>
  148. {
  149. return dialogForm.ShowDialog();
  150. }
  151. );
  152. Task task2 = Task.Factory.StartNew
  153. (
  154. () =>
  155. {
  156. try
  157. {
  158. //
  159. onProcessAction();
  160. r = 0;
  161. }
  162. catch (Exception e)
  163. {
  164. r = -1;
  165. if (onCaughtExceptionProcessAction != null)
  166. {
  167. onCaughtExceptionProcessAction(e);
  168. }
  169. }
  170. finally
  171. {
  172. TrySafeInvokeFormClose
  173. (
  174. dialogForm
  175. , onCaughtExceptionProcessAction
  176. );
  177. }
  178. try
  179. {
  180. onProcessedAction();
  181. }
  182. catch (Exception e)
  183. {
  184. //r = -1;
  185. onCaughtExceptionProcessAction(e);
  186. }
  187. finally
  188. {
  189. TrySafeInvokeFormClose(dialogForm, onCaughtExceptionProcessAction);
  190. }
  191. }
  192. );
  193. Task.WaitAny(task1, task2);
  194. //DialogResult dialogResult = await task;
  195. return r;
  196. }
  197. public static int ProcessWaitingShowDialog
  198. (
  199. IWin32Window ownerWindow
  200. , Form dialogForm
  201. , Action onProcessAction = null
  202. , Action onProcessedAction = null
  203. , Action<Exception> onCaughtExceptionProcessAction = null
  204. )
  205. {
  206. //var wait = new AutoResetEvent(false);
  207. int r = 1;
  208. if (onProcessAction != null)
  209. {
  210. new Thread
  211. (
  212. new ThreadStart
  213. (
  214. () =>
  215. {
  216. //wait.WaitOne();
  217. Thread.Sleep(10);
  218. try
  219. {
  220. //
  221. onProcessAction();
  222. r = 0;
  223. }
  224. catch (Exception e)
  225. {
  226. r = -1;
  227. if (onCaughtExceptionProcessAction != null)
  228. {
  229. onCaughtExceptionProcessAction(e);
  230. }
  231. }
  232. finally
  233. {
  234. TrySafeInvokeFormClose
  235. (
  236. dialogForm
  237. , onCaughtExceptionProcessAction
  238. );
  239. }
  240. try
  241. {
  242. onProcessedAction();
  243. }
  244. catch (Exception e)
  245. {
  246. //r = -1;
  247. onCaughtExceptionProcessAction(e);
  248. }
  249. finally
  250. {
  251. TrySafeInvokeFormClose(dialogForm, onCaughtExceptionProcessAction);
  252. }
  253. }
  254. )
  255. ).Start();
  256. //wait.Set();
  257. if (r != 0)
  258. {
  259. dialogForm.ShowDialog(ownerWindow);
  260. }
  261. }
  262. return r;
  263. }
  264. private static bool TrySafeInvokeFormClose
  265. (
  266. Form dialogForm
  267. , Action<Exception> onCaughtExceptionProcessAction
  268. )
  269. {
  270. bool r = false;
  271. try
  272. {
  273. if
  274. (
  275. dialogForm.IsHandleCreated
  276. && !dialogForm.IsDisposed
  277. )
  278. {
  279. dialogForm.Invoke
  280. (
  281. new Action
  282. (
  283. () =>
  284. {
  285. //try
  286. {
  287. if
  288. (
  289. dialogForm.IsHandleCreated
  290. && !dialogForm.IsDisposed
  291. )
  292. {
  293. dialogForm.Close();
  294. }
  295. //throw new Exception("理论上不应该被外侧 try catch 捕获?!?!?!?!?!");
  296. }
  297. /// catch (Exception e)
  298. /// {
  299. /// r = false;
  300. /// if (onCaughtExceptionProcessAction != null)
  301. /// {
  302. /// onCaughtExceptionProcessAction(e);
  303. /// }
  304. /// }
  305. }
  306. )
  307. );
  308. Thread.Sleep(10);
  309. }
  310. r = true;
  311. }
  312. catch (Exception e)
  313. {
  314. r = false;
  315. if (onCaughtExceptionProcessAction != null)
  316. {
  317. onCaughtExceptionProcessAction(e);
  318. }
  319. }
  320. return r;
  321. }
  322. public static int ProcessWaitingCancelable
  323. (
  324. Func<AutoResetEvent> onWaitFactoryFunc
  325. , Action onProcessAction
  326. , Action onProcessedAction
  327. , Action<Exception> onCaughtExceptionProcessAction
  328. )
  329. {
  330. var wait = onWaitFactoryFunc();
  331. return
  332. ProcessWaitingCancelable
  333. (
  334. wait
  335. , onProcessAction
  336. , onProcessedAction
  337. , onCaughtExceptionProcessAction
  338. );
  339. }
  340. public static int ProcessWaitingCancelable
  341. (
  342. AutoResetEvent wait
  343. , Action onProcessAction
  344. , Action onProcessedAction
  345. , Action<Exception> onCaughtExceptionProcessAction
  346. )
  347. {
  348. int r = 1; //Cancel
  349. new Thread
  350. (
  351. new ThreadStart
  352. (
  353. () =>
  354. {
  355. try
  356. {
  357. onProcessAction();
  358. r = 0;
  359. onProcessedAction();
  360. }
  361. catch (Exception e)
  362. {
  363. r = -1;
  364. onCaughtExceptionProcessAction(e);
  365. }
  366. finally
  367. {
  368. wait.Set();
  369. }
  370. }
  371. )
  372. ).Start();
  373. wait.WaitOne();
  374. return r;
  375. }
  376. }
  377. }
  378. namespace Microshaoft
  379. {
  380. using System;
  381. using System.Drawing;
  382. using System.ComponentModel;
  383. using System.Threading;
  384. using System.Windows.Forms;
  385. public class ProcessWaitingCancelableDialog : Form
  386. {
  387. private IContainer components = null;
  388. protected override void Dispose(bool disposing)
  389. {
  390. if (disposing && (components != null))
  391. {
  392. components.Dispose();
  393. }
  394. base.Dispose(disposing);
  395. }
  396. private void InitializeComponent()
  397. {
  398. button1 = new Button();
  399. SuspendLayout();
  400. //
  401. // button1
  402. //
  403. button1.DialogResult = DialogResult.Cancel;
  404. button1.Location = new Point(98, 158);
  405. button1.Name = "button1";
  406. button1.Size = new Size(75, 23);
  407. button1.TabIndex = 0;
  408. button1.Text = "取消(&C)";
  409. button1.UseVisualStyleBackColor = true;
  410. //
  411. // MainForm
  412. //
  413. AutoScaleDimensions = new SizeF(8F, 16F);
  414. AutoScaleMode = AutoScaleMode.Font;
  415. CancelButton = button1;
  416. ClientSize = new Size(282, 253);
  417. ControlBox = false;
  418. Controls.Add(button1);
  419. ///Name = "MainForm";
  420. ///Text = "MainForm";
  421. ResumeLayout(false);
  422. }
  423. private Button button1;
  424. public Button CancelWaitButton
  425. {
  426. get
  427. {
  428. return button1;
  429. }
  430. }
  431. public ProcessWaitingCancelableDialog()
  432. {
  433. InitializeComponent();
  434. button1.Click += button1_Click;
  435. }
  436. void button1_Click(object sender, EventArgs e)
  437. {
  438. button1.Click -= button1_Click;
  439. Close();
  440. }
  441. }
  442. }

Waiting Processed Cancelable ShowDialog (Release 2)的更多相关文章

  1. Waiting Processed Cancelable ShowDialog

    namespace ConsoleApplication { using System; using System.Threading; using Microshaoft; /// <summ ...

  2. Git工作流指南:Gitflow工作流 Comparing Workflows

    Comparing Workflows The array of possible workflows can make it hard to know where to begin when imp ...

  3. git workflows

    https://www.atlassian.com/git/tutorials/comparing-workflows Comparing Workflows The array of possibl ...

  4. 优先队列运用 TOJ 4123 Job Scheduling

    链接:http://acm.tju.edu.cn/toj/showp4123.html 4123.   Job Scheduling Time Limit: 1.0 Seconds   Memory ...

  5. PatentTips - Fair scalable reader-writer mutual exclusion

    BACKGROUND The present invention relates generally to multithreaded programming and, more specifical ...

  6. flutter Waiting for another flutter command to release the startup lock…

    flutter安装完成后执行flutter doctor ,一直提示如下: Waiting for another flutter command to release the startup loc ...

  7. Flutter报错 Waiting for another flutter command to release the startup lock...

    Waiting for another flutter command to release the startup lock… 异常解决 平时我们在开发flutter过程中,在执行flutter p ...

  8. 解决flutter 运行时:Waiting for another flutter command to release the startup lock...

    执行 Flutter 包管理相关命令时有可能遇到 Waiting for another flutter command to release the startup lock... 这样的错误,可尝 ...

  9. Waiting for another flutter command to release the startup lock...

    2019独角兽企业重金招聘Python工程师标准>>> rm ./flutter/bin/cache/lockfile info from 转载于:https://my.oschin ...

随机推荐

  1. centos7最小版本安装nginx+tomcat+java+mysql运行环境

    最近项目从windows搬到linux,由于项目组成员有限并且有其它紧急的任务需要处理,因而这个任务就落到我的头上了.下面记录下centos最小版本安装nginx+tomcat+mysql+java的 ...

  2. Android Broadcast 和 iOS Notification

    感觉以上2个机能有许多相似之处,作个记录,待研究!

  3. ACM/ICPC 之 BFS(离线)+康拓展开 (HDU1430-魔板)

    魔板问题,一道经典的康拓展开+BFS问题,为了实现方便,我用string类来表示字符串,此前很少用string类(因为不够高效,而且相对来说我对char数组的相关函数比较熟),所以在这里也发现了很多容 ...

  4. MySQL 5.6 Threadpool(优先队列)介绍及性能测试【转】

    本文来自:http://www.gpfeng.com/?p=540&utm_source=tuicool&utm_medium=referral 背景介绍 MySQL常用(目前线上使用 ...

  5. SAP ALV OO 选择行打印

    &---------------------------------------------------------------------* *& Report  ZSDF001 * ...

  6. Effective C++ -----条款32:确定你的public继承塑模出is-a关系

    “public继承”意味is-a.适用于base classes身上的每一件事情一定也适用于derived classes身上,因为每一个derive class对象也都是一个base class对象 ...

  7. codeforces 505A. Mr. Kitayuta's Gift 解题报告

    题目链接:http://codeforces.com/problemset/problem/505/A 题目意思:给出一个长度不大于10的小写英文字符串 s,问是否能通过在字符串的某个位置插入一个字母 ...

  8. 用原生DOM 遍历页面节点

    代码丢失,直接上图:

  9. Oracle 修改现有列的数据类型

    如果表中有数据,Oracle是不能修改其数据类型的.但可以通过新建一个临时列,将要修改列的数据复制到临时列中,删除原列再修改临时列的名字.这样说好像有点拗口,分步解说一下. 表AC_REG中有列:is ...

  10. odoo注销后在登录时的用户名和密码

    初识odoo时会遇到注销后无法登陆的情况,一般原因是没有留意管理员邮件地址和对应的密码所致.初始情况下默认的邮件地址为admin,密码为数据库创建时提供的密码.