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; } } } |