namespace ConsoleApplication
{
using System;
using System.Threading;
using Microshaoft;
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
//[STAThread]
static void Main(string[] args)
{
string input = string.Empty;
while ("q" != (input = Console.ReadLine()))
{
Test();
}
}
static void Test()
{
//
// TODO: 在此处添加代码以启动应用程序
//
Console.WriteLine("Hello World");
Console.WriteLine(Environment.Version.ToString());
var r = TaskProcesserHelper.WaitingProcessedCancelableShowDialog
(
null
, (x) =>
{
x.Text = "Test";
x.ShowInTaskbar = false;
}
, () =>
{
Thread.Sleep(5 * 1000);
}
, () =>
{
Console.WriteLine("Finished");
}
, (x) =>
{
Console.WriteLine
(
x
);
}
).ToString();
Console.WriteLine(r);
}
}
}
namespace Microshaoft
{
using System;
using System.Threading;
using System.Windows.Forms;
public static class TaskProcesserHelper
{
public static DialogResult WaitingProcessedCancelableShowDialog
(
IWin32Window owner
, Action<WaitingProcessedCancelableDialog> onDialogInitializeProcessAction
, Action onProcessAction
, Action onProcessedAction
, Action<Exception> onCaughtExceptionProcessAction
)
{
var form = new WaitingProcessedCancelableDialog();
onDialogInitializeProcessAction(form);
return
form.ShowDialog
(
owner
, () =>
{
Thread.Sleep(10);
onProcessAction();
}
, () =>
{
onProcessedAction();
try
{
if
(
form.IsHandleCreated
&& !form.IsDisposed
)
{
form.Invoke
(
new Action
(
() =>
{
try
{
if
(
form.IsHandleCreated
&& !form.IsDisposed
)
{
form.Close();
}
}
catch (Exception e)
{
onCaughtExceptionProcessAction(e);
}
}
)
);
}
}
catch (Exception e)
{
onCaughtExceptionProcessAction(e);
}
}
, (x) =>
{
onCaughtExceptionProcessAction(x);
}
);
}
public static int WaitingProcessedCancelable
(
Func<AutoResetEvent> onGetWaitingFunc
, Action onProcessAction
, Action onProcessedAction
, Action<Exception> onCaughtExceptionProcessAction
)
{
int r = 1; //Cancel
var wait = onGetWaitingFunc();
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 WaitingProcessedCancelableDialog : 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;
//
// Form1
//
AutoScaleDimensions = new SizeF(8F, 16F);
AutoScaleMode = AutoScaleMode.Font;
CancelButton = button1;
ClientSize = new Size(282, 253);
ControlBox = false;
Controls.Add(button1);
///Name = "Form1";
///Text = "Form1";
ResumeLayout(false);
}
private Button button1;
public Button CancelWaitButton
{
get
{
return button1;
}
}
public WaitingProcessedCancelableDialog()
{
InitializeComponent();
button1.Click += button1_Click;
}
void button1_Click(object sender, EventArgs e)
{
_wait.Set();
button1.Click -= button1_Click;
Close();
}
private object _objectLocker = new object();
private AutoResetEvent _wait = new AutoResetEvent(false);
private DialogResult _dialogResult = DialogResult.None;
public DialogResult ShowDialog
(
IWin32Window owner
, Action onBackgroundProcessAction
, Action onBackgroundProcessedAction
, Action<Exception> onCaughtExceptionProcessAction
)
{
new Thread
(
new ThreadStart
(
() =>
{
int r = TaskProcesserHelper.WaitingProcessedCancelable
(
() => _wait
, () =>
{
//Thread.Sleep(10 * 10);
onBackgroundProcessAction();
}
, () =>
{
onBackgroundProcessedAction();
}
, (x) =>
{
onCaughtExceptionProcessAction(x);
}
);
switch (r)
{
case 1 : //Cancel
_dialogResult = DialogResult.Cancel;
break;
case 0 :
_dialogResult = DialogResult.OK;
//Close();
/// try
/// {
/// if
/// (
/// IsHandleCreated
/// && !IsDisposed
/// )
/// {
/// Invoke
/// (
/// new Action
/// (
/// () =>
/// {
/// if
/// (
/// IsHandleCreated
/// && !IsDisposed
/// )
/// {
/// try
/// {
/// Close();
/// }
/// catch (Exception e)
/// {
/// onCaughtExceptionProcessAction(e);
/// }
/// }
/// }
/// )
/// );
/// }
/// }
/// catch (Exception e)
/// {
/// onCaughtExceptionProcessAction(e);
/// }
break;
case -1 :
_dialogResult = DialogResult.Abort;
break;
}
}
)
).Start();
base.ShowDialog(owner);
return _dialogResult;
}
}
}

Waiting Processed Cancelable ShowDialog的更多相关文章

  1. Waiting Processed Cancelable ShowDialog (Release 2)

    namespace Test { using System; using System.Windows.Forms; static class Program { /// <summary> ...

  2. 1014. Waiting in Line (30)

    Suppose a bank has N windows open for service. There is a yellow line in front of the windows which ...

  3. PAT 1014. Waiting in Line

    Suppose a bank has N windows open for service.  There is a yellow line in front of the windows which ...

  4. A1014. Waiting in Line

    Suppose a bank has N windows open for service. There is a yellow line in front of the windows which ...

  5. PAT A1014 Waiting in Line (30 分)——队列

    Suppose a bank has N windows open for service. There is a yellow line in front of the windows which ...

  6. PAT Waiting in Line[转载]

    //转自:https://blog.csdn.net/apie_czx/article/details/45537627 1014 Waiting in Line (30)(30 分)Suppose ...

  7. 1014 Waiting in Line (30)(30 point(s))

    problem Suppose a bank has N windows open for service. There is a yellow line in front of the window ...

  8. PTA (Advanced Level) 1014 Waiting in Line

    Waiting in Line Suppose a bank has N windows open for service. There is a yellow line in front of th ...

  9. PAT 1014 Waiting in Line (模拟)

    1014. Waiting in Line (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppo ...

随机推荐

  1. 文件上传(js, C#)

    ajaxFileUpload http://www.cnblogs.com/kissdodog/archive/2012/12/15/2819025.html

  2. Java 抽象类与oop三大特征

    面向对象主要有三大特性:继承和多态.封装. 一.抽象类 在了解抽象类之前,先来了解一下抽象方法.抽象方法是一种特殊的方法:它只有声明,而没有具体的实现.抽象方法的声明格式为: abstract voi ...

  3. XsltListViewWebPart 和自定义列表视图

    http://msdn.microsoft.com/zh-cn/library/ff806162(v=office.14).aspx

  4. POJ 2676

    http://poj.org/problem?id=2676 深搜的题目. 题意呢就是一个数独的游戏,应该都知道规则. 思路:我的思路很简单,就是用数组来判断某个数字是否可以使用,而每一个数字都由三个 ...

  5. default constructor,copy constructor,copy assignment

     C++ Code  12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 ...

  6. sed小知识总结

    1)sed默认是打印出文件中的所有行的,使用 -n 选项可以只打印出 匹配 的行 2)当用到sed不同的编辑命令时,用{},且不同编辑命令之间用分号

  7. string literals may have at most 255 elements

    一行中的字符超出 255 时候发生这个错误 解决方式: 一行的字符串分成两行 并 + 连接 **** 将字符串分成两个用"+"连接起来

  8. nyoj_34_韩信点兵

     中国剩余定理: 代码: #include <iostream> #include <cstdio> using namespace std; int main() { int ...

  9. Divide and conquer:Drying(POJ 3104)

    烘干衣服 题目大意:主人公有一个烘干机,但是一次只能烘干一件衣服,每分钟失水k个单位的水量,自然烘干每分钟失水1个单位的水量(在烘干机不算自然烘干的那一个单位的水量),问你最少需要多长时间烘干衣服? ...

  10. 针对SYN洪水攻击的防御措施

    可以运用sysctl命令进行配置,由于本命令参数较多,这里只简单记录几个比较常用的参数: 1.tcp_max_syn_backlog 这个参数指定了后备队列可维持的TCP半开连接的数目,如果该值设定很 ...