Example #1
0
        /// <summary>
        /// 重置(把IsSend通通改為flase)
        /// </summary>
        private void btnReSet_Click(object sender, EventArgs e)
        {
            MessageBoxButtons buttons = MessageBoxButtons.OK;
            DialogResult      result  = MessageBox.Show("確認是否重置?", "確認", buttons);

            if (result == DialogResult.OK)
            {
                string strMessage = "";
                try
                {
                    List <TestLibrary.Models.MEDM> li = new TestLibrary.BLL.BEDM().GetList();
                    foreach (TestLibrary.Models.MEDM mod in li)
                    {
                        mod.IsSend = false;
                        new TestLibrary.BLL.BEDM().Edit(mod);;
                    }
                    strMessage = "重置成功";
                    LoadData();
                }
                catch (Exception ex)
                {
                    strMessage = ex.ToString();
                }
                finally
                {
                    MessageBox.Show(strMessage);
                }
            }
        }
Example #2
0
        private void LoadData()
        {
            //因為重置而加的
            progressBar1.Value          = 0;
            btnStartBackgroundWork.Text = "寄信";
            checkedListBox1.Items.Clear();

            //設定是否暫停
            isSuspend = true;
            backgroundWorker1.WorkerReportsProgress      = true; //声明异步执行的时候可以报告进度
            backgroundWorker1.WorkerSupportsCancellation = true; //声明可以异步取消

            //BUTTON設定
            this.btnStartBackgroundWork.Enabled  = true;
            this.btnCancelBackgroundWork.Enabled = false;
            //this.btnTempStop.Enabled = false;

            try
            {
                //取得寄信名單
                List <TestLibrary.Models.MEDM> li = new TestLibrary.BLL.BEDM().GetList();
                int intSelect = 0;
                if (li.Count > 0)
                {
                    foreach (TestLibrary.Models.MEDM mod in li)
                    {
                        //listBox1.Items.Add(mod.NameC);
                        checkedListBox1.Items.Add(mod.NameC + " " + mod.Email);
                        if (mod.IsSend)
                        {
                            checkedListBox1.SetItemChecked(checkedListBox1.Items.Count - 1, true);
                            intSelect++;
                        }
                        else
                        {
                            checkedListBox1.SetItemChecked(checkedListBox1.Items.Count - 1, false);
                        }
                    }
                }
                //設定百分比
                _Limit         = li.Count;
                lbPrecent.Text = intSelect.ToString() + " / " + _Limit;

                double  doui       = Convert.ToDouble(intSelect);
                double  douAll     = Convert.ToDouble(_Limit);
                double  douPercent = Math.Round(doui / douAll, 2);
                int     intcorrect = Convert.ToInt32(Math.Round(douPercent * 100));
                decimal dec        = (Convert.ToDecimal(intSelect) / Convert.ToDecimal(_Limit)) * 100;
                lbl_State.Text     = intcorrect.ToString() + "%";
                progressBar1.Value = intcorrect;
                //lbl_State.Invoke((Action<int>)UpdateStateLabel, intcorrect);
                //取得edm內容
                GetEDMContent();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
Example #3
0
        /// <summary>
        /// 在背景所執行的動作
        /// </summary>
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                if (backgroundWorker1.CancellationPending)
                {
                    //直接告诉程序已经执行完了,在实际的项目中,我们可以在界面显示“强行终止”
                    backgroundWorker1.ReportProgress(0, "强行终止");
                    return;
                }
                //先取得
                TestLibrary.BLL.BEDM bll = new TestLibrary.BLL.BEDM();
                DataSet ds = bll.GetEDMList();

                //信件內容
                string strContent;
                using (FileStream source = new FileStream(strEDMPath + strEDMFile, FileMode.Open, FileAccess.Read))
                {
                    System.IO.Stream       stream       = source;
                    System.IO.StreamReader StreamReader = new StreamReader(stream, System.Text.Encoding.GetEncoding(950));
                    strContent = StreamReader.ReadToEnd();
                }

                //信件標題
                string strSubject = tbSubject.Text.Trim();

                int i = 0;
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    if (backgroundWorker1.CancellationPending)
                    {
                        //e.Cancel = true;
                        backgroundWorker1.ReportProgress(0, "");
                        return;
                    }
                    else
                    {
                        //開始寄信
                        if (!Convert.ToBoolean(dr["IsSend"]))
                        {
                            while (isSuspend)
                            {
                                Thread.Sleep(50);
                            }
                            Thread.Sleep(60 * 1000);

                            //寄件人
                            //string strSendMan = "*****@*****.**";
                            string strSendMan = dr["Email"].ToString();
                            bool   IsSend     = TestLibrary.Common.Util.SendMail(strSendMan, strSubject, strContent);
                            //bool IsSend = true;
                            if (IsSend)
                            {
                                //如果寄出成功,勾選成功
                                checkedListBox1.Invoke((Action <int>)cbCheck, i);

                                //更新狀態到Sql server
                                string strMemberID = dr["MemberID"].ToString();
                                bll.EditByID(strMemberID);
                            }
                            else
                            {
                                intError++;
                                if (intError == 10)
                                {
                                    intError = 0;
                                    Thread.Sleep(300 * 1000);
                                }
                            }
                        }

                        //向主线程报告进度(我们将i当作进度传递给主线程,当循环1次,我们就当做完成1%的工作,并报告给主线程)
                        double doui       = Convert.ToDouble(i + 1);
                        double douAll     = Convert.ToDouble(_Limit);
                        double douPercent = Math.Round(doui / douAll, 2);
                        backgroundWorker1.ReportProgress(Convert.ToInt32(Math.Round(douPercent * 100)), "");
                        lbPrecent.Invoke((Action <int>)SetPrecent, i);
                        i++;
                    }
                }

                string            message = "寄信完成";
                string            title   = "完成";
                MessageBoxButtons buttons = MessageBoxButtons.OK;
                DialogResult      result  = MessageBox.Show(message, title, buttons);
                if (result == DialogResult.OK)
                {
                    if (!backgroundWorker1.IsBusy)
                    {
                        LoadData();
                    }
                }
            }
            catch (Exception ex)
            {
                if (backgroundWorker1.IsBusy)//如果DoWork处于忙状态,才执行该方法
                {
                    backgroundWorker1.CancelAsync();
                    btnStartBackgroundWork.Enabled = false;
                    bool IsCancel = backgroundWorker1.CancellationPending;
                    //MessageBox.Show("已取消寄信");
                }
                DialogResult result = MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK);
                if (result == DialogResult.OK)
                {
                    LoadData();
                }
            }
        }