Esempio n. 1
0
        /// <summary>
        /// 窗体异步等待执行
        /// </summary>
        /// <param name="from">需要异步等待的窗体</param>
        /// <param name="waitMethod">等待执行的方法(注:方法中不能含有对UI线程控件的操作)</param>
        /// <param name="callBackMethod">等待执行完成回调方法</param>
        public static void AsyncWaitDo(this Form form, Action waitMethod, Action callBackMethod)
        {
            List <Control> controlList = new List <Control>();

            foreach (Control control in form.Controls)
            {
                if (control.Enabled)
                {
                    control.Enabled = false;
                    controlList.Add(control);
                }
            }

            AsyncWaitControl waitControl = new AsyncWaitControl();

            waitControl.Location = new Point((form.Width - waitControl.Width) / 2, (form.Height - waitControl.Height) / 2);
            waitControl.Anchor   = AnchorStyles.None;
            form.Controls.Add(waitControl);
            form.ActiveControl = waitControl;
            waitControl.BringToFront();
            form.Refresh();

            Task.Factory.StartNew(() =>
            {
                waitMethod();
            }).ContinueWith((t) =>
            {
                #region waitMethod执行完成后
                if (form.IsHandleCreated)
                {
                    form.BeginInvoke(new Action(() =>
                    {
                        if (t.Exception != null)
                        {
                            #region waitMethod发生异常时
                            form.Controls.Remove(waitControl);
                            waitControl.Dispose();
                            Exception ex = t.Exception.InnerException;
                            MessageBox.Show(ex.Message);
                            #endregion
                        }
                        else
                        {
                            #region waitMethod未发生异常
                            try
                            {
                                if (callBackMethod != null)
                                {
                                    callBackMethod();
                                }
                            }
                            finally
                            {
                                form.Controls.Remove(waitControl);
                                waitControl.Dispose();
                                controlList.ForEach(f => f.Enabled = true);
                            }
                            #endregion
                        }
                    }));
                }
                else if (t.Exception != null)
                {
                    MessageBox.Show(t.Exception.InnerException.Message);
                }
                #endregion
            });
        }
Esempio n. 2
0
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                string str_name  = "";
                int    i_success = 0;
                int    i_fail    = 0;
                i_sendcount = 0;
                i_rowcount  = dataGridView1.Rows.Count;
                string           str_return_message = "";
                AsyncWaitControl a = new AsyncWaitControl();
                this.AsyncWaitDo(delegate
                {
                    if (str_title == "基本")
                    {
                        for (int i = 0; i < dataGridView1.Rows.Count; i++)
                        {
                            str_return_message = SendMessageByJiben(dataGridView1.Rows[i]);
                            addLog(dataGridView1.Rows[i].Cells["姓名"].Value.ToString() + "," + dataGridView1.Rows[i].Cells["电话号码"].Value.ToString() + "," + str_return_message);
                            if (str_return_message != "OK")
                            {
                                str_name = str_name + dataGridView1.Rows[i].Cells["姓名"].Value.ToString();
                                i_fail++;
                            }
                            else
                            {
                                i_success++;
                            }

                            i_sendcount++;
                        }
                    }
                    else if (str_title == "绩效")
                    {
                        for (int i = 0; i < dataGridView1.Rows.Count; i++)
                        {
                            str_return_message = SendMessageByJixiao(dataGridView1.Rows[i]);
                            addLog(dataGridView1.Rows[i].Cells["姓名"].Value.ToString() + "," + dataGridView1.Rows[i].Cells["电话号码"].Value.ToString() + "," + str_return_message);
                            if (str_return_message != "OK")
                            {
                                str_name = str_name + dataGridView1.Rows[i].Cells["姓名"].Value.ToString();
                                i_fail++;
                            }
                            else
                            {
                                i_success++;
                            }
                            i_sendcount++;
                        }
                    }
                },
                                 delegate
                {
                    MessageBox.Show("发送完成!成功:" + i_success.ToString() + "条,失败:" + i_fail.ToString() + "条。详情请查看日志!");
                });
            }
            catch (Exception ex)
            {
                MessageBox.Show("表格格式不正确!");
            }
        }