BackgroundWorker 使用

BackgroundWorker 使用
private void uploadBgWork_DoWork(object sender, DoWorkEventArgs e)
        {
            var file = e.Argument.ToString();
            if (!File.Exists(file)) return;
            var info = new ReportUploadInfo
            {
                Remark = meRemark.Text.Trim(),
                ExpTime =
                    string.IsNullOrEmpty(txDays.Text)
                        ? DateTime.MaxValue.Date
                        : DateTime.Now.AddDays(double.Parse(txDays.Text)).Date,
                DecompressionKey = tbPass.Text.Trim(),
                UserEmail = teEmail.Text.Trim(),
                UserPhone = tePhone.Text.Trim()
            };
            var result = FtpHelper.UploadFile(
                new FileInfo(file), 
                DateTime.Now.Year.ToString(), 
                DateTime.Now.Month.ToString(), 
                SystemConst.FtpAddr, 
                SystemConst.FtpUserName, 
                SystemConst.FtpPassWord, 
                uploadBgWork, info);
            if (uploadBgWork.CancellationPending)
                e.Cancel = true;
            else
                e.Result = result;
        }
 private void uploadBgWork_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar2.Text = (int.Parse(progressBar2.Text) + e.ProgressPercentage).ToString();
            if (e.UserState != null)
                lblMsg.Text = e.UserState.ToString();
        }
private void uploadBgWork_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            UploadOkSetStatus();
            if (e.Error != null)
            {
                XtraMessageBox.Show(e.Error.Message);
                Logger.WriteLog(e.Error.Message);
                return;
            }
            if (e.Cancelled)
            {
                XtraMessageBox.Show("文件上传已经取消!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                bool result;
                if (bool.TryParse(e.Result.ToString(), out result))
                {
                    if (result)
                    {
                        XtraMessageBox.Show("上传成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        XtraMessageBox.Show("失败:上传失败", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        Logger.WriteLog("失败:上传失败" + btnSelectFile.Text);
                    }
                }
                else
                {
                    XtraMessageBox.Show("错误:未知的返回值", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    Logger.WriteLog("错误:未知的返回值"+ btnSelectFile.Text);
                }
            }
        }

 

posted @ 2018-08-16 21:40  micwin  阅读(103)  评论(0编辑  收藏  举报