关于委托处理问题的求教

找到一段程序,看到委托操作的地方不太理解,希望朋友们多多指教。特别是委托操作的执行流程或处理机制不理解。

ContractedBlock.gifExpandedBlockStart.gif程序如下 :
  1None.gifusing System;
  2None.gifusing System.Collections.Generic;
  3None.gifusing System.ComponentModel;
  4None.gifusing System.Data;
  5None.gifusing System.Drawing;
  6None.gifusing System.Text;
  7None.gifusing System.Windows.Forms;
  8None.gif
  9None.gifnamespace CH5
 10ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 11InBlock.gif    public partial class CH5_DemoForm001 : Form
 12ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 13InBlock.gif        public CH5_DemoForm001()
 14ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 15InBlock.gif            InitializeComponent();
 16ExpandedSubBlockEnd.gif        }

 17InBlock.gif
 18InBlock.gif        private void CH5_DemoForm001_Load(object sender, EventArgs e)
 19ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 20InBlock.gif            RefreshOpenFormCount();
 21ExpandedSubBlockEnd.gif        }

 22InBlock.gif
 23InBlock.gif        private void btnGetOpenForms_Click(object sender, EventArgs e)
 24ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 25InBlock.gif            // 开启数个窗体并将它们最小化。
 26InBlock.gif           CH5_DemoForm002 myCH5_DemoForm002 = new CH5_DemoForm002();
 27InBlock.gif            myCH5_DemoForm002.Show();
 28InBlock.gif            myCH5_DemoForm002.WindowState = FormWindowState.Minimized;
 29InBlock.gif
 30InBlock.gif            CH5_DemoForm004 myCH5_DemoForm004 = new CH5_DemoForm004();
 31InBlock.gif            myCH5_DemoForm004.Show();
 32InBlock.gif            myCH5_DemoForm004.WindowState = FormWindowState.Minimized;
 33InBlock.gif
 34InBlock.gif            CH5_DemoForm005 myCH5_DemoForm005 = new CH5_DemoForm005();
 35InBlock.gif            myCH5_DemoForm005.Show();
 36InBlock.gif            myCH5_DemoForm005.WindowState = FormWindowState.Minimized;
 37InBlock.gif
 38InBlock.gif            // 取得所有已开启之窗体的标题。
 39InBlock.gif            GetOpenFormTitles();
 40InBlock.gif
 41InBlock.gif            // 取得已开启的窗体数目。
 42InBlock.gif            RefreshOpenFormCount();
 43ExpandedSubBlockEnd.gif        }

 44InBlock.gif
 45InBlock.gif        private void GetOpenFormTitles()
 46ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 47InBlock.gif            try
 48ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 49InBlock.gif                string myFormTitle;
 50InBlock.gif
 51InBlock.gif                foreach (Form f in Application.OpenForms)
 52ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 53InBlock.gif                    // 使用一个进程安全的方法来取得所有已打开的窗体的标题。
 54InBlock.gif                    myFormTitle = GetFormTitle(f);
 55InBlock.gif
 56InBlock.gif                    if (!(this.ListBox1.Items.Contains(myFormTitle)))
 57ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
 58InBlock.gif                        this.ListBox1.Items.Add(myFormTitle);
 59ExpandedSubBlockEnd.gif                    }

 60ExpandedSubBlockEnd.gif                }

 61ExpandedSubBlockEnd.gif            }

 62InBlock.gif            catch (Exception ex)
 63ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 64InBlock.gif                this.ListBox1.Items.Add("错误:" + ex.Message);
 65ExpandedSubBlockEnd.gif            }

 66ExpandedSubBlockEnd.gif        }

 67InBlock.gif
 68InBlock.gif        private delegate string GetFormTitleDelegate(Form f);
 69InBlock.gif        private string GetFormTitle(Form f)
 70ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 71InBlock.gif            // 检查窗体是否能够从目前的进程来访问。
 72InBlock.gif            if (!(f.InvokeRequired))
 73ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 74InBlock.gif                // 直接访问窗体。
 75InBlock.gif                return f.Text;
 76ExpandedSubBlockEnd.gif            }

 77InBlock.gif            else
 78ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 79InBlock.gif                // 封送处理至拥有窗体的进程。
 80InBlock.gif                GetFormTitleDelegate del = GetFormTitle;
 81ExpandedSubBlockStart.gifContractedSubBlock.gif                object[] param = dot.gif{ f };
 82InBlock.gif                System.IAsyncResult result = f.BeginInvoke(del, param);
 83InBlock.gif
 84InBlock.gif                // 让窗体的进程有机会来处理函数。
 85InBlock.gif                System.Threading.Thread.Sleep(10);
 86InBlock.gif
 87InBlock.gif                // 检查结果。
 88InBlock.gif                if (result.IsCompleted)
 89ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 90InBlock.gif                    // 取得函式的传回值。
 91InBlock.gif                    return "不同的进程:" + f.EndInvoke(result).ToString();
 92ExpandedSubBlockEnd.gif                }

 93InBlock.gif                else
 94ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 95InBlock.gif                    return "没有响应的进程";
 96ExpandedSubBlockEnd.gif                }

 97ExpandedSubBlockEnd.gif            }

 98ExpandedSubBlockEnd.gif        }

 99InBlock.gif
100InBlock.gif        private void RefreshOpenFormCount()
101ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
102InBlock.gif            this.lblOpenFormsCount.Text = Application.OpenForms.Count.ToString();
103ExpandedSubBlockEnd.gif        }

104InBlock.gif
105ExpandedSubBlockEnd.gif    }

106ExpandedBlockEnd.gif}

107None.gif
108None.gif



其中 :

ContractedBlock.gifExpandedBlockStart.gif
None.gifprivate delegate string GetFormTitleDelegate(Form f);
None.gif        
private string GetFormTitle(Form f)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            
// 检查窗体是否能够从目前的进程来访问。
InBlock.gif
            if (!(f.InvokeRequired))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
// 直接访问窗体。
InBlock.gif
                return f.Text;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
// 封送处理至拥有窗体的进程。
InBlock.gif
                GetFormTitleDelegate del = GetFormTitle;
ExpandedSubBlockStart.gifContractedSubBlock.gif                
object[] param = dot.gif{ f };
InBlock.gif                System.IAsyncResult result 
= f.BeginInvoke(del, param);
InBlock.gif
InBlock.gif                
// 让窗体的进程有机会来处理函数。
InBlock.gif
                System.Threading.Thread.Sleep(10);
InBlock.gif
InBlock.gif                
// 检查结果。
InBlock.gif
                if (result.IsCompleted)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
// 取得函式的传回值。
InBlock.gif
                    return "不同的进程:" + f.EndInvoke(result).ToString();
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return "没有响应的进程";
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }


这段代码中的,
GetFormTitleDelegate del = GetFormTitle;
object[] param = { f };
System.IAsyncResult result = f.BeginInvoke(del, param);
System.Threading.Thread.Sleep(10);
这几句,不理解C#是怎么工作的,特别是
System.IAsyncResult result = f.BeginInvoke(del, param);
这句,完全不明白程序执行做了些什么。

其它基本上都明白!我感觉还是不明白委托的工作原理。 急求教!

转载于:https://www.cnblogs.com/154691780/archive/2007/05/21/754881.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值