namespace Test
{
using System;
using System.Windows.Forms;
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
}
namespace Test
{
using System;
using System.Threading;
using System.Windows.Forms;
using Microshaoft;
partial class MainForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(119, 74);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(282, 253);
this.Controls.Add(this.button1);
this.Name = "MainForm";
this.Text = "MainForm";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button button1;
}
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var r = TaskProcesserHelper.ProcessWaitingShowDialog40
(
this
, new ProcessWaitingCancelableDialog()
, () =>
{
Thread.Sleep(5 * 1000);
//throw new Exception();
}
, () =>
{
Console.WriteLine("Finished");
}
, (x) =>
{
Console.WriteLine("Caught Exception: {0}", x);
}
);
Console.WriteLine(r);
}
}
}
namespace Microshaoft
{
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
public static class TaskProcesserHelper
{
public static int ProcessWaitingShowDialog
(
IWin32Window ownerWindow
, Func<Form> onWaitingDialogFactoryFunc
, Action onProcessAction = null
, Action onProcessedAction = null
, Action<Exception> onCaughtExceptionProcessAction = null
)
{
var dialogForm = onWaitingDialogFactoryFunc();
return
ProcessWaitingShowDialog
(
ownerWindow
, dialogForm
, onProcessAction = null
, onProcessedAction = null
, onCaughtExceptionProcessAction = null
);
}
public static int ProcessWaitingShowDialog40
(
IWin32Window ownerWindow
, Form dialogForm
, Action onProcessAction = null
, Action onProcessedAction = null
, Action<Exception> onCaughtExceptionProcessAction = null
)
{
var r = 1;
Task<DialogResult> task1 = Task.Factory.StartNew<DialogResult>
(
() =>
{
return dialogForm.ShowDialog();
}
);
Task task2 = Task.Factory.StartNew
(
() =>
{
try
{
//
onProcessAction();
r = 0;
}
catch (Exception e)
{
r = -1;
if (onCaughtExceptionProcessAction != null)
{
onCaughtExceptionProcessAction(e);
}
}
finally
{
TrySafeInvokeFormClose
(
dialogForm
, onCaughtExceptionProcessAction
);
}
try
{
onProcessedAction();
}
catch (Exception e)
{
//r = -1;
onCaughtExceptionProcessAction(e);
}
finally
{
TrySafeInvokeFormClose(dialogForm, onCaughtExceptionProcessAction);
}
}
);
Task.WaitAny(task1, task2);
//DialogResult dialogResult = await task;
return r;
}
public static int ProcessWaitingShowDialog
(
IWin32Window ownerWindow
, Form dialogForm
, Action onProcessAction = null
, Action onProcessedAction = null
, Action<Exception> onCaughtExceptionProcessAction = null
)
{
//var wait = new AutoResetEvent(false);
int r = 1;
if (onProcessAction != null)
{
new Thread
(
new ThreadStart
(
() =>
{
//wait.WaitOne();
Thread.Sleep(10);
try
{
//
onProcessAction();
r = 0;
}
catch (Exception e)
{
r = -1;
if (onCaughtExceptionProcessAction != null)
{
onCaughtExceptionProcessAction(e);
}
}
finally
{
TrySafeInvokeFormClose
(
dialogForm
, onCaughtExceptionProcessAction
);
}
try
{
onProcessedAction();
}
catch (Exception e)
{
//r = -1;
onCaughtExceptionProcessAction(e);
}
finally
{
TrySafeInvokeFormClose(dialogForm, onCaughtExceptionProcessAction);
}
}
)
).Start();
//wait.Set();
if (r != 0)
{
dialogForm.ShowDialog(ownerWindow);
}
}
return r;
}
private static bool TrySafeInvokeFormClose
(
Form dialogForm
, Action<Exception> onCaughtExceptionProcessAction
)
{
bool r = false;
try
{
if
(
dialogForm.IsHandleCreated
&& !dialogForm.IsDisposed
)
{
dialogForm.Invoke
(
new Action
(
() =>
{
//try
{
if
(
dialogForm.IsHandleCreated
&& !dialogForm.IsDisposed
)
{
dialogForm.Close();
}
//throw new Exception("理论上不应该被外侧 try catch 捕获?!?!?!?!?!");
}
/// catch (Exception e)
/// {
/// r = false;
/// if (onCaughtExceptionProcessAction != null)
/// {
/// onCaughtExceptionProcessAction(e);
/// }
/// }
}
)
);
Thread.Sleep(10);
}
r = true;
}
catch (Exception e)
{
r = false;
if (onCaughtExceptionProcessAction != null)
{
onCaughtExceptionProcessAction(e);
}
}
return r;
}
public static int ProcessWaitingCancelable
(
Func<AutoResetEvent> onWaitFactoryFunc
, Action onProcessAction
, Action onProcessedAction
, Action<Exception> onCaughtExceptionProcessAction
)
{
var wait = onWaitFactoryFunc();
return
ProcessWaitingCancelable
(
wait
, onProcessAction
, onProcessedAction
, onCaughtExceptionProcessAction
);
}
public static int ProcessWaitingCancelable
(
AutoResetEvent wait
, Action onProcessAction
, Action onProcessedAction
, Action<Exception> onCaughtExceptionProcessAction
)
{
int r = 1; //Cancel
new Thread
(
new ThreadStart
(
() =>
{
try
{
onProcessAction();
r = 0;
onProcessedAction();
}
catch (Exception e)
{
r = -1;
onCaughtExceptionProcessAction(e);
}
finally
{
wait.Set();
}
}
)
).Start();
wait.WaitOne();
return r;
}
}
}
namespace Microshaoft
{
using System;
using System.Drawing;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;
public class ProcessWaitingCancelableDialog : Form
{
private IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
button1 = new Button();
SuspendLayout();
//
// button1
//
button1.DialogResult = DialogResult.Cancel;
button1.Location = new Point(98, 158);
button1.Name = "button1";
button1.Size = new Size(75, 23);
button1.TabIndex = 0;
button1.Text = "取消(&C)";
button1.UseVisualStyleBackColor = true;
//
// MainForm
//
AutoScaleDimensions = new SizeF(8F, 16F);
AutoScaleMode = AutoScaleMode.Font;
CancelButton = button1;
ClientSize = new Size(282, 253);
ControlBox = false;
Controls.Add(button1);
///Name = "MainForm";
///Text = "MainForm";
ResumeLayout(false);
}
private Button button1;
public Button CancelWaitButton
{
get
{
return button1;
}
}
public ProcessWaitingCancelableDialog()
{
InitializeComponent();
button1.Click += button1_Click;
}
void button1_Click(object sender, EventArgs e)
{
button1.Click -= button1_Click;
Close();
}
}
}

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. AngularJs之ng-repeat的用法

    可参考文章:http://blog.csdn.net/renfufei/article/details/43061877 ng-repeat信息展示的核心: [1]异步读取数据源 works,见代码一 ...

  2. java 调用webservice的各种方法总结

    java 调用webservice的各种方法总结 几种流行的开源WebService框架Axis1,Axis2,Xfire,CXF,JWS比较 方法一:创建基于JAX-WS的webservice(包括 ...

  3. Longest Increasing Path in a Matrix

    Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...

  4. 深入理解 Win32 PE 文件格式

    深入理解 Win32 PE 文件格式 Matt Pietrek 这篇文章假定你熟悉C++和Win32. 概述 理解可移植可执行文件格式(PE)可以更好地了解操作系统.如果你知道DLL和EXE中都有些什 ...

  5. eclipse 工作环境配置

    1.更换编辑颜色, http://eclipse-color-theme.github.io/update/ 下载离线安装包,解压缩 eclipse-color-theme-update-site\u ...

  6. 在Linux下记录所有用户的登录和操作日志

    一般我们可以用history命令来查看用户的操作记录,但是这个命令不能记录是哪个用户登录操作的,也不能记录详细的操作时间,且不完整:所以误操作而造成重要的数据丢失,就很难查到是谁操作的. 在这里我们通 ...

  7. Divide and Conquer:Monthly Expense(POJ 3273)

    Monthly Expense 题目大意:不废话,最小化最大值 还是直接套模板,不过这次要注意,是最小化最大值,而不是最大化最小值,判断的时候要注意 联动3258 #include <iostr ...

  8. [Android进阶]学习AccessibilityService实现微信抢红包插件

    在你的手机更多设置或者高级设置中,我们会发现有个无障碍的功能,很多人不知道这个功能具体是干嘛的,其实这个功能是为了增强用户界面以帮助残障人士,或者可能暂时无法与设备充分交互的人们 它的具体实现是通过A ...

  9. HDU 5878 I Count Two Three (打表+二分查找) -2016 ICPC 青岛赛区网络赛

    题目链接 题意:给定一个数n,求大于n的第一个只包含2357四个因子的数(但是不能不包含其中任意一种),求这个数. 题解:打表+二分即可. #include <iostream> #inc ...

  10. 51nod 1070 Bash游戏 V4 (斐波那契博弈)

    题目:传送门. 有一堆个数为n(n>=2)的石子,游戏双方轮流取石子,规则如下: 1)先手不能在第一次把所有的石子取完,至少取1颗: 2)之后每次可以取的石子数至少为1,至多为对手刚取的石子数的 ...