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. 《Head First Servlet JSP》学习笔记

  2. meteor 为基础,联合 Apollo + React + React-Router

    Graphql with Apollo, Meteor and React: https://janikvonrotz.ch/2016/10/09/graphql-with-apollo-meteor ...

  3. struts2 servlet api 访问方式

    Action中访问ServletAPI. 主要就是接收表单参数及向域对象中存取值. 关于SevletAPI的方法在Action中有三种方式: 1.完全解耦合的形式: * 使用一个类:ActionCon ...

  4. Python 之 【markdown 模块的学习】

    摘要: markdown工具,可以将txt转化成html格式.这一类工具的作用是将按一定格式写成的可读性强的文本文件转化为结构化的标准xhtml或html.Linux 下面也有markdown: zh ...

  5. 将项目导入eclipse中出现的jsp页面报错解决

  6. C# 串口操作系列(2) -- 入门篇,为什么我的串口程序在关闭串口时候会死锁 ?

    第一篇文章我相信很多人不看都能做的出来,但是,用过微软SerialPort类的人,都遇到过这个尴尬,关闭串口的时候会让软件死锁.天哪,我可不是武断,算了.不要太绝对了.99.9%的人吧,都遇到过这个问 ...

  7. PDO(数据访问抽象层)

    自带事务功能,多条sql同时执行时,如果其中一条执行失败,那么所有的都执行失败.开启了事务,可以进行回滚操作,让程序变得更安全. 1.访问不同的数据库2.自带事务功能3.防止SQL注入:分两次发送 / ...

  8. php date函数 参数详细

    time()在PHP中是得到一个数字,这个数字表示从1970-01-01到现在共走了多少秒,很奇怪吧 不过这样方便计算, 要找出前一天的时间就是 time()-60*60*24; 要找出前一年的时间就 ...

  9. HTML简历表格

    效果图 <!DOCTYPE > <html> <head> <meta charset="utf-8" /> </head&g ...

  10. October 6th 2016 Week 41st Thursday

    The outer world you see is a reflection of your inner self. 你拥有什么样的内心,你就会看到什么样的世界. And we eventually ...