首页 新闻 会员 周边

winfrom BackGroundWorker控件 取消没反映

0
悬赏园豆:20 [已关闭问题] 关闭于 2013-10-18 11:45

namespace SoftPhoneXO
{
    public partial class FormImportExcel : Form
    {
        public FormImportExcel()
        {
            InitializeComponent();
        }

        //private ManualResetEvent manualReset = new ManualResetEvent(true);
        //int count = 30;
        private void button2_Click(object sender, EventArgs e)
        {
            if (backgroundWorker1.CancellationPending == true)
            {
                this.backgroundWorker1.CancelAsync();

            }
            //this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bool resultAll = false;
            resultAll = CRMHelper.DeleteClientOrderAll();
            this.lblImport.Text = "正在导入请等待";
            if (backgroundWorker1.IsBusy != true)
            {
                // 启动异步操作
                backgroundWorker1.RunWorkerAsync();
            }

          
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();
            op.ShowDialog();
            this.txtfileName.Text = op.FileName;
        }

        bool result = false;
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            //BackgroundWorker worker = sender as BackgroundWorker;
            //InsertData(worker);
            ExcelOP excelop = new ExcelOP();//Excel操作类
            string filename = this.txtfileName.Text;
            bool bl = false;
            if (filename != "")
            {
                try
                {
                    DataSet ds = excelop.ConvertToTable(filename);
                    foreach (DataTable item in ds.Tables)
                    {
                        for (int i = 3; i < item.Rows.Count; i++)
                        {
                            if (item.Rows[i][0].ToString() != "[null]" && string.IsNullOrEmpty(item.Rows[i][0].ToString()) == false)
                            {
                                bl = ImportExcelHelp.ImportDataFromExcel(item.Rows[i]);
                            }
                        }
                        Thread.Sleep(60);
                    }

                }
                catch (Exception ex)
                {
                    MessageBox.Show("出现未知错误!请重试!" + ex.ToString());
                    result = CRMHelper.DeleteClient();
                    result = CRMHelper.DeleteClientOrder();
                    result = CRMHelper.DeleteTbMsm();
                    if (result)
                    {
                        MessageBox.Show("请核实Excel格式和数据是否准确");
                    }
                }
            }
            else
            {
                MessageBox.Show("请选择文件再导入!谢谢合作!");
            }
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                MessageBox.Show(e.Error.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.lblImport.Text = "导入结束";
                //MessageBox.Show(e.Error.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else if (e.Cancelled)
            {
                bool cancelResult = false;
                this.lblImport.Text = "导入结束";
                MessageBox.Show("取消操作!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                cancelResult = CRMHelper.DeleteClient();
                cancelResult = CRMHelper.DeleteClientOrder();
                cancelResult = CRMHelper.DeleteTbMsm();
                return;
            }
            else
            {
                if (result == false)
                {
                    this.lblImport.Text = "导入结束";
                    MessageBox.Show("导入成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    //MessageBox.Show("操作成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    this.lblImport.Text = "导入结束";
                    MessageBox.Show("导入失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }

        }
    }

winnnnnner的主页 winnnnnner | 初学一级 | 园豆:47
提问于:2013-06-06 12:22
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册