1
yhj
2024-07-24 5e5d945e91568b973faa27d8ab0bcef99fc4a6c5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#region
 
using System;
using System.Threading;
using System.Windows.Forms;
using CSFrameworkV5.Common;
using CSFrameworkV5.Core;
 
#endregion
 
namespace CSFrameworkV5.Library
{
    /// <summary>
    ///     异步线程处理通用界面(异步操作等待窗体)
    /// </summary>
    public partial class frmThreadOperating : frmBase
    {
        private static frmThreadOperating _Instance;
 
        private static Thread _Thread;
        private static Form _Owner;
        private static object _Params; //MyThreadExecuting事件的参数
 
        private static MyThreadExecuting _OnOperating;
        private static MyThreadFinished _OnFinished;
        private static MyThreadAborting _OnAborting;
 
        //计时器读秒
        private int _Secondes;
 
        private frmThreadOperating()
        {
            InitializeComponent();
        }
 
        private void btnStop_Click(object sender, EventArgs e)
        {
            //若用户提供_OnAborting事件,在线程结束前,要通知该事件。
            if (_OnAborting != null || _OnAborting())
            {
                if (_Thread != null) _Thread.Abort(); //结束线程
 
                _Thread = null;
                _Instance = null;
                Close();
            }
        }
 
        /// <summary>
        ///     关闭窗体
        /// </summary>
        /// <param name="owner"></param>
        public static void CloseMe(Form owner)
        {
            if (_Instance != null)
                try
                {
                    _Instance.Hide();
                    _Instance.Close();
                    _Instance = null;
                    owner.Show();
                    GC.Collect();
                }
                catch
                {
                }
        }
 
        private void DoCloseMe()
        {
            Close();
            _Instance = null;
        }
 
        private void frmThreadOperating_Load(object sender, EventArgs e)
        {
            //开始计时
            _Secondes = 1;
            timer1.Start();
 
            //启动线程
            _Thread = new Thread(OnThreadStart);
            _Thread.Start();
        }
 
        private void frmWaitingThread_FormClosing(object sender,
            FormClosingEventArgs e)
        {
            timer1.Stop();
            labelControl1.Text = "系统正在中断线程.....";
            Application.DoEvents();
 
            if (_Thread != null && _Thread.ThreadState == ThreadState.Running)
            {
                if (Msg.AskQuestion("确定要取消操作吗?"))
                {
                    //若用户提供_OnAborting事件,在线程结束前,要通知该事件。
                    if (_OnAborting != null || _OnAborting())
                    {
                        e.Cancel = false;
                        _Thread.Abort();
                        _Thread = null;
                    }
                }
                else
                {
                    e.Cancel = true;
                }
            }
 
            if (!e.Cancel)
            {
                _Instance = null;
                _Thread = null;
 
                GC.Collect();
            }
        }
 
        private void OnThreadStart()
        {
            object result = null;
 
            try
            {
                //开始操作,比如查询大数据、批量处理大数据
                result = _OnOperating(_Params);
 
                //关闭当前窗体
                if (_Instance != null)
                    _Instance.Invoke(new MyThreadClose(_Instance.DoCloseMe));
 
                //操作完成,异步通知用户已完成数据处理
                if (_Owner != null && _OnFinished != null)
                    _Owner.Invoke(new MyThreadFinished(_OnFinished), result);
 
                _Thread = null;
            }
            catch (Exception ex) //线程异常事件处理
            {
                //if (ex.Message != "正在中止线程。") Msg.ShowException(ex);
 
                //线程结束事件
                if (_OnFinished != null)
                    _Owner.Invoke(new MyThreadFinished(_OnFinished), result);
 
                //关闭窗体
                if (_Instance != null)
                    _Instance.Invoke(new MyThreadClose(_Instance.DoCloseMe));
 
                _Thread = null;
            }
        }
 
        /// <summary>
        ///     显示线程操作窗体
        /// </summary>
        /// <param name="owner">父级窗体,打开线程操作窗体</param>
        /// <param name="doSomething">执行操作</param>
        /// <param name="parameters">传入参数</param>
        /// <param name="aborting">取消线程通知</param>
        /// <param name="finished">线程完成后回调事件</param>
        /// <param name="windowCaption">设置窗体标题</param>
        public static void ShowMe(
            Form owner,
            MyThreadExecuting doSomething,
            object parameters = null,
            MyThreadAborting aborting = null,
            MyThreadFinished finished = null,
            string windowCaption = "")
        {
            if (_Instance == null)
            {
                _Owner = owner;
                _OnOperating = doSomething;
                _Params = parameters;
                _OnFinished = finished;
                _OnAborting = aborting;
                _Instance = new frmThreadOperating();
                _Instance.Text = windowCaption.IsNullOrEmpty()
                    ? "异步操作等待窗体"
                    : windowCaption;
                _Instance.ShowDialog(owner);
            }
            else
            {
                Msg.Warning("异步任务正在运行中,请完成此任务后再操作!");
            }
        }
 
        private void timer1_Tick(object sender, EventArgs e)
        {
            labelControl2.Text = "用时 " + _Secondes.ToStringEx() + " 秒";
            _Secondes++;
        }
 
        #region 自定义委托类型
 
        public delegate object MyThreadExecuting(object param);
 
        public delegate void MyThreadFinished(object data);
 
        public delegate bool MyThreadAborting(); //线程终止事件,必须返回True;
 
        public delegate void MyThreadClose();
 
        #endregion
    }
}