惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

云风的 BLOG
云风的 BLOG
GbyAI
GbyAI
G
Google Developers Blog
Engineering at Meta
Engineering at Meta
月光博客
月光博客
腾讯CDC
Recent Announcements
Recent Announcements
酷 壳 – CoolShell
酷 壳 – CoolShell
爱范儿
爱范儿
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
S
SegmentFault 最新的问题
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
阮一峰的网络日志
阮一峰的网络日志
博客园 - 【当耐特】
The GitHub Blog
The GitHub Blog
Last Week in AI
Last Week in AI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
aimingoo的专栏
aimingoo的专栏
Google DeepMind News
Google DeepMind News
Y
Y Combinator Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Martin Fowler
Martin Fowler
A
About on SuperTechFans
博客园 - 叶小钗

博客园 - nametmp

【转】Resource与Resx的区别 - nametmp - 博客园 异步调用过程 Winform用匿名方法新建线程的方法 enum枚举用法笔记 对using的新认识 视频文件信息读取器 琐碎记录 【转贴】GridView里面的HYPERLINK数据邦定 【转贴】数据邦定语法 - nametmp - 博客园 TableAdapter、Dataset与BindingSource的关系 webform 可视化在gridview中加入其它控件 WinForm 加入登陆窗体(c#) winform 中的 datagridview 添加 progressbar列 和 calendar 列 远程删除Kaspersky(被Kaspersky搞了一天) SqlServer2000登陆角色的具体权限 终于用sqlcmd连上sqlserver2005express了 sqlserver操作摘录 [转]SQL Server 2005中的SQLCMD工具使用 [转]sqlserver2005(Express版)的配置
对C#委托的理解
nametmp · 2008-07-10 · via 博客园 - nametmp

我对C#委托的理解一直是模糊的,今天好像有点感觉,特把它记录下来。如果说的不对,请各位不吝赐教。
委托——形似“函数”的“类级”“函数指针”,三个关键:1、形式上像函数,2、属于类一级,3、本质是函数指针,是一座调用其他函数桥梁。以下是代码解析:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Threading;

namespace WebBrowser
{
    //1、声明:形式像函数,只是多了关键字delegate;因为是类级,所以其位置与其他类并列
    delegate void dUpdateStatus(string url);  

    public partial class FormMain : Form
    {       
        Thread tRecord;
       
        public FormMain()
        {
            InitializeComponent();

            tRecord = new Thread(new ParameterizedThreadStart(Record));
        }

        private void btRecord_Click(object sender, EventArgs e)
        {
            tRecord.Start();
        }

        private void btStop_Click(object sender, EventArgs e)
        {
            tRecord.Abort();
        }

        private void Record(object url)
        {
         
            url = "http://www.sina.com.cn/**.shtml";
          
            //2、指向被委托的方法: 可以用匿名方法填充 或 指向命名的方法
            dUpdateStatus du = new dUpdateStatus
                (
                    //这里用匿名方法来填充委托,其输入的参数要与委托的参数一致
                    delegate(string urlcatch)
                    {
                        tbStatus.Text += urlcatch + "\r\n";
                    }
                );

            //3、使用:直接调用 或 被调用
            //du(entry.URL);                 //直接调用。因本线程不是ui线程,会提示错误
            this.BeginInvoke(du, url); //被begininvoke调用,异步更新ui
            //this.Invoke(du, entry.URL);    //被invoke     调用,同步更新ui,会阻塞本线程
            }        
        }
    }
}

posted on 2008-07-10 12:01  nametmp  阅读(836)  评论()    收藏  举报