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. C# nullable<T> 用法小结

    今天在园子里看到一个关于C#中对于可空类型的描述的帖子,感觉不错于是自己写了个小例子尝试下. 在C#中,对于可空类型描述为:Nullable<T>, 它表示该类型是可以为空的一个类型.它被 ...

  2. MySQL中无GROUP BY直接HAVING的问题【转】

    本文来自网址: http://www.penglixun.com/tech/database/having_without_groupby_in_mysql.html 今天有同学给我反应,有一张表,i ...

  3. 在Debian上用Bind 配置DNS服务器

    1 什么是DNS 初学者可能不理解DNS到底是什么,干什么用.我是在1998年大学毕业时才听说这个词的.那时我在聊天室碰到潍坊信息港的一个网管,我恬不知耻地说我也是个网管,他说也维护DNS吗?我说,D ...

  4. CodeSnippetsLibrary的使用方法

    在项目开发中,我们经常可以看到如下所示的代码: @property (nonatomic, copy) NSString *isbatchapprove; @property (nonatomic,  ...

  5. s:iterator,s:if与OGNL的嵌套使用

    今天在写代码时,遇到个如下问题,要求当前登陆用户的id与系统参数类型代码所属维护人的id相同时,显示单选框.如下效果: 代码如下: <s:iterator value="vo.page ...

  6. CocoaPods安装教程

    Code4App 原创文章.转载请注明出处:http://code4app.com/article/cocoapods-install-usage CocoaPods是什么? 当你开发iOS应 用时, ...

  7. GCD的使用

    什么是 GCD Grand Central Dispatch (GCD) 是 Apple 开发的一个多核编程的解决方法.该方法在 Mac OS X 10.6 雪豹中首次推出,并随后被引入到了 iOS4 ...

  8. 解决webstorm乱码

    新的web前端学习群,120342833,欢迎大家一起学习,以前在web学习群里的看到了加下..

  9. Centos7 设置Swap分区

    1.使用dd命令创建一个swap交换文件 dd if=/dev/zero of=/home/swap bs=1024 count=1024000 2.制作为swap格式文件: mkswap /home ...

  10. Stanford大学机器学习公开课(二):监督学习应用与梯度下降

    本课内容: 1.线性回归 2.梯度下降 3.正规方程组   监督学习:告诉算法每个样本的正确答案,学习后的算法对新的输入也能输入正确的答案   1.线性回归 问题引入:假设有一房屋销售的数据如下: 引 ...