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. 【K8s】Kubernetes 最近正在看的资料

    中国移动Kubernetes多集群统一管理实践:  http://www.tuicool.com/articles/FrqQrqI#c-22517 一种新的进入容器的方式: WebSocket + D ...

  2. 【GoLang】GO语言系列--002.GO语言基础

    002.GO语言基础 1 参考资料 1.1 http://www.cnblogs.com/vimsk/archive/2012/11/03/2736179.html 1.2 https://githu ...

  3. ThreadPool线程池 小结

    ThreadPool类提供一个线程池,该线程池可用于发送工作项.处理异步 I/O.代表其他线程等待以及处理计时器 线程池通过为应用程序提供一个由系统管理的辅助线程池使您可以更为有效地使用线程.一个线程 ...

  4. display:inline 遇上 li 无效? why?

    若制作导航栏时,使用列表li 的定义时,若想加上一个背景图 ,这时候若定义li的一个属性为:li{display:inline ; width:83px; height:30px;},则浏览器会无视后 ...

  5. MAC OS X 常用通用快捷键

    注:由于使用的是Windows键盘,习惯了Ctrl + c/v复制粘贴,所以修改了修饰键,Command(⌘)键和Control(^)键互换,以下的Ctrl键均为Command键,对应键盘上的实际左C ...

  6. 用基础动画实现iOS控件循环旋转

    - (void)viewDidLoad { [super viewDidLoad]; UIButton* ag=[[UIButton alloc]initWithFrame:CGRectMake(sc ...

  7. uva 401.Palindromes

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  8. code vs1706 求合数和(数论 素数的判定)

    1706 求合数和  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 白银 Silver 题解  查看运行结果     题目描述 Description 用户输入一个数,然后输出 ...

  9. 【leetcode】 Palindrome Partitioniong (middle) (*^__^*)

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  10. IOS searchBar去掉背景

    修改UISearchBar的背景颜色 UISearchBar是由两个subView组成的,一个是UISearchBarBackGround,另一个是UITextField. 要IB中没有直接操作背景的 ...