程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C# 多線程傳參和返回值委托異步回調實例

C# 多線程傳參和返回值委托異步回調實例

編輯:C#入門知識

[csharp] 
       
 delegate bool MyDelete(TreeNodeCollection node); 
    bool prrelust = false; 
        bool xzjh = false; 
     bool prrelust = false; 
        bool xzjh = false; 
        public bool PrintTreeViewNode(TreeNodeCollection node) 
        { 
 
 
            foreach (TreeNode n in node) 
            { 
 
 
                if (n.Checked &&(Convert.ToInt32(n.Tag)==30 )) 
                { 
                    xzjh = true; 
                    string fanode = n.Parent.Parent.Text; 
                    string time = cbTimeDelay.Text.Trim().Substring(0, cbTimeDelay.Text.Trim().Length - 1); //延時  
                    int jihao = Convert.ToInt32(fanode.Substring(fanode.IndexOf("(") + 1, fanode.IndexOf(")") - fanode.IndexOf("(") - 1));//機號  
                    int menhao;//門號  
                    GCR.DoorSedBll bl = new GCR.DoorSedBll(); 
 
 
                    if (n.Parent.Text != "主機") 
                    { 
                        menhao = Convert.ToInt32(n.Parent.Tag) * 8 + 4 + n.Index; 
                    } 
                    else 
                    { 
                        menhao = n.Index;//門號  
                    } 
                    prrelust = bl.OpenDoorTimedelaySet(jihao, menhao, Convert.ToInt32(time)); 
 
 
 
 
 
 
                    if (!prrelust) 
                    { 
                        label3.Text = ""; 
                        break; 
                    } 
 
 
                } 
                PrintTreeViewNode(n.Nodes); 
            } 
 
 
 
 
            return prrelust; 
 
 
 
 
 
 
        } 
        #endregion 
 
 
  private void btOpenDoorTimeSet_Click(object sender, EventArgs e) 
        { 
            label3.Text = "延時加載中,請稍等......."; 
            MyDelete Dele = new MyDelete(PrintTreeViewNode); 
            Dele.BeginInvoke(this.tvDevc.Nodes, new AsyncCallback(AfterMyMothod), null); 
 
 
        } 
        #endregion 
 
 
        #region 異步傳參回調函數  
        public void AfterMyMothod(IAsyncResult result) 
        { 
 
 
            AsyncResult async = (AsyncResult)result; 
            MyDelete DelegateInstance = (MyDelete)async.AsyncDelegate; 
            bool rt = DelegateInstance.EndInvoke(result); 
            if (xzjh) 
            { 
 
 
                if (rt) 
                { 
                    MessageBox.Show("延時加載成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); 
 
 
                } 
                else 
                { 
                    MessageBox.Show("延時加載失敗!,請重新加載!", "錯誤提示", MessageBoxButtons.OK, MessageBoxIcon.Error); 
 
 
                } 
            } 
            else 
            { 
 
 
                MessageBox.Show("請選擇控制機號", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); 
            } 
            label3.Text = ""; 
        } 
        #endregion 

     
 delegate bool MyDelete(TreeNodeCollection node);
    bool prrelust = false;
        bool xzjh = false;
     bool prrelust = false;
        bool xzjh = false;
        public bool PrintTreeViewNode(TreeNodeCollection node)
        {


            foreach (TreeNode n in node)
            {


                if (n.Checked &&(Convert.ToInt32(n.Tag)==30 ))
                {
                    xzjh = true;
                    string fanode = n.Parent.Parent.Text;
                    string time = cbTimeDelay.Text.Trim().Substring(0, cbTimeDelay.Text.Trim().Length - 1); //延時
                    int jihao = Convert.ToInt32(fanode.Substring(fanode.IndexOf("(") + 1, fanode.IndexOf(")") - fanode.IndexOf("(") - 1));//機號
                    int menhao;//門號
                    GCR.DoorSedBll bl = new GCR.DoorSedBll();


                    if (n.Parent.Text != "主機")
                    {
                        menhao = Convert.ToInt32(n.Parent.Tag) * 8 + 4 + n.Index;
                    }
                    else
                    {
                        menhao = n.Index;//門號
                    }
                    prrelust = bl.OpenDoorTimedelaySet(jihao, menhao, Convert.ToInt32(time));

 

 


                    if (!prrelust)
                    {
                        label3.Text = "";
                        break;
                    }


                }
                PrintTreeViewNode(n.Nodes);
            }

 


            return prrelust;

 

 


        }
        #endregion


  private void btOpenDoorTimeSet_Click(object sender, EventArgs e)
        {
            label3.Text = "延時加載中,請稍等.......";
            MyDelete Dele = new MyDelete(PrintTreeViewNode);
            Dele.BeginInvoke(this.tvDevc.Nodes, new AsyncCallback(AfterMyMothod), null);


        }
        #endregion


        #region 異步傳參回調函數
        public void AfterMyMothod(IAsyncResult result)
        {


            AsyncResult async = (AsyncResult)result;
            MyDelete DelegateInstance = (MyDelete)async.AsyncDelegate;
            bool rt = DelegateInstance.EndInvoke(result);
            if (xzjh)
            {


                if (rt)
                {
                    MessageBox.Show("延時加載成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);


                }
                else
                {
                    MessageBox.Show("延時加載失敗!,請重新加載!", "錯誤提示", MessageBoxButtons.OK, MessageBoxIcon.Error);


                }
            }
            else
            {


                MessageBox.Show("請選擇控制機號", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            label3.Text = "";
        }
        #endregion

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved