WinForm委託修改界面,異步處理無響應。

WinForm委託修改界面,異步處理無響應。


using System;
using System.Collections.Generic;
using System.Threading; 
using System.Windows.Forms;
using BJYS.Business;
using BJYS.Utilities;
using Platform; 
using SCBJ.Win.impl; 

namespace SCBJ.Win
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            ResetUi();
            InitCbox();
        } 
        private bool _isRun = false;
        private delegate void GetOrdersDelegate(DateTime tpStart,DateTime tpEnd,int qid);
        private delegate void BaoJiaDelegate(MallYinOrder parm);
        /// <summary>
        /// 執行按鈕
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_todo_Click(object sender, EventArgs e)
        { 
            ResetUi();
            var tpStart = this.dateTP_start.Value;
            var tpEnd = this.dateTP_end.Value;
            var qid = ((Info)this.comboBox1.SelectedItem).Id;
            if (tpStart >= tpEnd)
            {
                MessageBox.Show("結束時間必須大於開始時間!");
                return;
            }
            this.btn_todo.Enabled = false;
            _isRun = true; 
            new GetOrdersDelegate(GetOrders).BeginInvoke(tpStart, tpEnd, qid, null, null);
        }
        /// <summary>
        /// 獲取訂單
        /// </summary>
        private void GetOrders(DateTime tpStart, DateTime tpEnd, int qid)
        { 
            var list = DomainControllerFactory.Create<OrderBll>().GetMallYinOrderSCBJ(null, tpStart, tpEnd, qid);
            var msg = string.Format("找到{0}條訂單數據。", list.Count);
            Action<String> actionInvoke = delegate (string n) { blb_Msg.Text = n; };
            blb_Msg.Invoke(actionInvoke, new object[] { msg });
            Program.log.Debug(msg); 
            _pAll = list.Count;
            foreach (var itemObj in list)
            {
                if (_isRun)
                {
                    new BaoJiaDelegate(BaoJia).BeginInvoke(itemObj, BaoJiaComplete, null); 
                }
                else break; 
                Thread.Sleep(200);
            }
            if (_pAll==0) ResetUi(); 
        }

        /// <summary>
        /// 測試報價
        /// </summary> 
        /// <param name="itemObj"></param>
        private void BaoJia(MallYinOrder itemObj)
        {
            if (itemObj.baojialog == null)
            {
                Program.log.Debug(string.Format("訂單號{0}沒有找到baojialog", itemObj.Id));
            }
            else if (!itemObj.baojialog.hjg.Contains("燙金") && !itemObj.baojialog.hjg.Contains("燙銀"))
            {
                string baojiaStr = string.Empty;
                if (itemObj.baojialog.qid == 21 && IsStandard(itemObj.baojialog.xx, itemObj.baojialog.yy))
                    baojiaStr = PinBusinessCard.GetStr(itemObj.baojialog);
                else if (itemObj.baojialog.qid == 22)
                    baojiaStr = PinDanZhang.GetStr(itemObj.baojialog);
                if (!baojiaStr.IsNullOrEmpty())
                {
                    BaoJiaServiceClient client = new BaoJiaServiceClient();
                    tempuri.org.ParamsObj pObj = new tempuri.org.ParamsObj();
                    pObj.QID = itemObj.baojialog.qid.ToString();
                    pObj.UID = itemObj.baojialog.uid.ToString();
                    pObj.Data = baojiaStr;
                    pObj = client.BaoJia(pObj);
                    var xmlResult = pObj.ProcessedResult;
                    var xmlTb = XMLHelper.XMLToDataTable(xmlResult);
                    var logidStr = xmlTb.Rows[0]["logid"].ToString();
                    Program.log.Debug(string.Format("**用戶:{3},訂單號:{0},logid:{4},參數字符串:{1},報價結果:{2}",
                        itemObj.Id, baojiaStr, xmlResult, pObj.UID, logidStr)); 
                    var logid = 0;
                    int.TryParse(logidStr, out logid);
                    //if (logid > 0)
                    //{
                    //    var bjlEntity = DomainControllerFactory.Create<baojialogBll>().Getbaojialog(logid);
                    //    var yoEntity = RF.Concrete<MallYinOrderRepository>().GetById(itemObj.Id);
                    //    if (yoEntity != null)
                    //    {
                    //        yoEntity.cbprice = bjlEntity.cbprice;
                    //        //RF.Save(yoEntity);
                    //        Program.log.Debug(string.Format("訂單號:{0},修改成本價爲:{1}", itemObj.Id, yoEntity.cbprice));
                    //    }
                    //    else
                    //    {
                    //        Program.log.Debug(string.Format("訂單號:{0},找不到訂單", itemObj.Id));
                    //    }
                    //}
                }
                else
                {
                    Program.log.Debug(string.Format("訂單號{0}生成的參數字符串爲空", itemObj.Id));
                }
            }
        }

        #region 進度條
        private int _pIndex = 1;
        private double _pCo = 0;
        private double _ppp = 0;
        private int _pAll = 17;
        /// <summary>
        /// 報價完成回調
        /// </summary>
        /// <param name="ir"></param>
        private void BaoJiaComplete(IAsyncResult ir)
        {
            Action<String> actionInvoke = delegate(string n)
            {
                if (_pIndex == _pAll)
                {
                    this.lbl_Progress_Text.Text = "完成";
                    this.panel_Progress_bg.Width = 600;
                    this.panel_Progress.Width = 600;
                    this.btn_todo.Enabled = true; //還原按鈕啓用 
                }
                else
                {
                    double p = 600d / _pAll;
                    _pCo += p;
                    _pIndex++;
                    this.lbl_Progress_Text.Text = Math.Round((_pCo / 600d * 100d), 2) + "%";
                    this.panel_Progress_bg.Width = 600;
                    _ppp += (600d/_pAll);
                    this.panel_Progress.Width = Convert.ToInt32(Math.Round(_ppp));
                }
            };
            panel_Progress.Invoke(actionInvoke, new object[] { "" });
        }
        /// <summary>
        /// 界面復位
        /// </summary>
        private void ResetUi()
        {
            this.panel_Progress_bg.Width = 0;
            this.panel_Progress.Width = 0;
            this.lbl_Progress_Text.Text = "";
            this.btn_todo.Enabled = true;
        }
        #endregion
        /// <summary>
        /// 停止
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Stop_Click(object sender, EventArgs e)
        {
            _isRun = false;
            this.btn_todo.Enabled = true;
        }
        
        private void InitCbox()
        {
            IList<Info> infoList = new List<Info>();
            Info info1 = new Info() { Id = 21, Name = "拼版名片" };
            Info info2 = new Info() { Id = 22, Name = "拼版單張" }; 
            infoList.Add(info1);
            infoList.Add(info2); 
            comboBox1.DataSource = infoList;
            comboBox1.ValueMember = "Id";
            comboBox1.DisplayMember = "Name";
        }
        /// <summary>
        /// 是否標準尺寸
        /// </summary>
        /// <param name="xx"></param>
        /// <param name="yy"></param>
        /// <returns></returns>
        private bool IsStandard(int? xx, int? yy)
        {
            bool falg = false;
            if (xx == 90 && yy == 50)
            {
                falg = true;
            }
            else if (xx == 90 && yy == 54)
            {
                falg = true;
            }
            else if (xx == 90 && yy == 95)
            {
                falg = true;
            }
            else if (xx == 90 && yy == 108)
            {
                falg = true;
            }
            else if (xx == 180 && yy == 54)
            {
                falg = true;
            }
            return falg;
        }
    }
    public class Info
    {
        public int Id { get; set; }
        public string Name { get; set; } 
    }
}





Action<string,string> actionInvoke = delegate (string message,string message2) { blb_Msg.Text = message; };
            blb_Msg.Invoke(actionInvoke, msg, msg);


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章