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. JavaScript Window对象

    1.Window对象的location属性引用的是Location对象,它表示该窗口中当前显示的文档的URL,并定义了方法来使窗口载入新的文档.Location对象的href属性是一个字符串,后者包含 ...

  2. SQL merge into 表合并

    Merge关键字是一个神奇的DML关键字.它在SQL Server 2008被引入,它能将Insert,Update,Delete简单的并为一句.MSDN对于Merge的解释非常的短小精悍:”根据与源 ...

  3. Graph Valid Tree

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

  4. ACM/ICPC 之 DP-基因相似度(POJ1080-ZOJ1027)

    题意:两端基因片段,各有明确的碱基序列,现有一个碱基匹配的相似度数组,设计程序使得该相似度最大. //POJ1080-ZOJ1027 //题解:将s1碱基和s2碱基看做等长,添加一个碱基为'-',即每 ...

  5. Python字符编码

    http://www.runoob.com/python/python-strings.html ASCII Unicode UTF-8 # -*- coding: utf-8 -*- 格式化 %运算 ...

  6. K3安装记录

    安装的某些客户端没有客户端管理工具,测试问题所在 K3安装目录选择到D盘就可以,它自动生成kingdee\k3路径

  7. codeforces 499B.Lecture 解题报告

    题目链接:http://codeforces.com/problemset/problem/499/B 题目意思:给出两种语言下 m 个单词表(word1, word2)的一一对应,以及 profes ...

  8. Android Multimedia框架总结(二)MediaPlayer框架及播放网络视频案例

    前言:前面一篇我们介绍MediaPlayer相关方法,有人说,没有实际例子,看得不是很明白,今天在分析MediaPlayer时,顺带一个播放网络视频例子.可以自行试试.今天分析的都是下几篇介绍各个模块 ...

  9. ios电话/密码/验证码/身份证的正则表达式

    // 一 .电话号码正则表达式 -(BOOL)testPhoneNumber:(NSString *)text { NSString *regex =@"(13[0-9]|0[1-9]|0[ ...

  10. 根据复选框checkbox的选中状态来打开或关闭隐藏层

    HTML:  <input type="checkbox" id="check-expert"> <div id="expert&q ...