当前位置:编程学习 > C#/ASP.NET >>

C#委托基础——Invoke与委托

 

C#委托基础系列原于2011年2月份发表在我的新浪博客中,现在将其般至本博客。

 

例一

[csharp] 
delegate void AppendStringCallback(string text); 
 
private void AppendString(string txt) 

     this.listView1.Items.Add(txt); 

 
  
 
private void ReceiveDate() 
 

 
     AppendStringCallback appendStringCallback = new AppendStringCallback(AppendString); 
 
     this.Invoke(appendStringCallback, new object[] { string.Format("{0},{1},{2}", str1, str2 
 
                 + "号", iepAddress.ToString()) }); 
 

delegate void AppendStringCallback(string text);

private void AppendString(string txt)
{
     this.listView1.Items.Add(txt);
}

 

private void ReceiveDate()

{

     AppendStringCallback appendStringCallback = new AppendStringCallback(AppendString);

     this.Invoke(appendStringCallback, new object[] { string.Format("{0},{1},{2}", str1, str2

                 + "号", iepAddress.ToString()) });

}


 

 

例二

[csharp]
namespace ThreadPoolDemo   
{   
    public partial class ThreadForm : Form   
    {   
        // 定义delegate以便Invoke时使用    
        private delegate void SetProgressBarValue(int value);    
        
 
        // 跟SetProgressBarValue委托相匹配的方法    
        private void SetProgressValue(int value)   
       {   
           progressBar.Value = value;   
       } 
 
 
        // 使用Invoke方法来设置进度条    
        private void RunWithInvoke()   
        {   
            int value = progressBar.Value;   
            while (value< progressBar.Maximum)   
            {   
                //如果是跨线程调用    
                if (InvokeRequired)   
                {   
                    this.Invoke(new SetProgressBarValue(SetProgressValue), value++);   
                }   
                else  
                {   
                    progressBar.Value = ++value;   
                }   
            }   
        }    
 
 
        public ThreadForm()   
        {   
            InitializeComponent();   
        }    
     
        private void btnInvoke_Click(object sender, EventArgs e)   
        {   
            progressBar.Value = 0;   
            Thread thread = new Thread(new ThreadStart(RunWithInvoke));   
            thread.Start();   
        }    
 
     }   

namespace ThreadPoolDemo 

    public partial class ThreadForm : Form 
    { 
        // 定义delegate以便Invoke时使用 
        private delegate void SetProgressBarValue(int value);  
      

        // 跟SetProgressBarValue委托相匹配的方法 
        private void SetProgressValue(int value) 
       { 
           progressBar.Value = value; 
       }


        // 使用Invoke方法来设置进度条 
        private void RunWithInvoke() 
        { 
            int value = progressBar.Value; 
            while (value< progressBar.Maximum) 
            { 
                //如果是跨线程调用 
                if (InvokeRequired) 
                { 
                    this.Invoke(new SetProgressBarValue(SetProgressValue), value++); 
                } 
                else
                { 
       &nbs

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,