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

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
Help Net Security
Help Net Security
P
Privacy International News Feed
T
Threat Research - Cisco Blogs
C
Cisco Blogs
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
L
LINUX DO - 热门话题
Security Latest
Security Latest
A
Arctic Wolf
G
GRAHAM CLULEY
月光博客
月光博客
S
Securelist
D
Docker
J
Java Code Geeks
T
Troy Hunt's Blog
T
Tenable Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
SecWiki News
SecWiki News
S
Security @ Cisco Blogs
量子位
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
L
LINUX DO - 最新话题
Recent Commits to openclaw:main
Recent Commits to openclaw:main
aimingoo的专栏
aimingoo的专栏
博客园 - 【当耐特】
H
Heimdal Security Blog
The Hacker News
The Hacker News
博客园 - 三生石上(FineUI控件)
Application and Cybersecurity Blog
Application and Cybersecurity Blog
N
Netflix TechBlog - Medium
Vercel News
Vercel News
Forbes - Security
Forbes - Security
B
Blog RSS Feed
H
Hackread – Cybersecurity News, Data Breaches, AI and More
IT之家
IT之家
B
Blog
MongoDB | Blog
MongoDB | Blog
博客园 - 聂微东
Google DeepMind News
Google DeepMind News
S
Secure Thoughts
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Check Point Blog
云风的 BLOG
云风的 BLOG
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
T
The Blog of Author Tim Ferriss
L
Lohrmann on Cybersecurity
F
Full Disclosure
D
Darknet – Hacking Tools, Hacker News & Cyber Security
P
Proofpoint News Feed

博客园 - 谢小漫

弹出选择窗体控件(附源码) 获取枚举描述信息(Description)2 - 谢小漫 - 博客园 mshtml组件引用的问题 导出所有用户表到excel Sql Server数据导出EXCEL C#的串口编程 DataReceived XML-RPC.NET的X509Certificates如何使用呢 XML-RPC.NET 在读《C#和.NET 2.0实战》 如何在ASP.NET中做异步 C#的串口编程 重新开始学习.net 在看Test-Driven Development In Microsoft .NET 最近了解过的一个支付接口 网站的第三天 BAIDU的第一个搜索访问进入网站 不错的日期选择 星期六提交sitemap 关于代码自动生成器
如何在winform中用委托做异步
谢小漫 · 2008-12-09 · via 博客园 - 谢小漫

不好意思,直接上代码了。

Code
delegate void VoidDelegate ();
VoidDelegate vd 
= DataBind;
vd.EndInvoke(vd.BeginInvoke(
nullnull));


所以delegate(委托)的基本东西还是要会的。有返回时候的后续操作的版本是:

Code
delegate DataView DataViewDelegate();private void button1_Click(object sender, EventArgs e)
{
DataViewDelegate dvd 
= new DataViewDelegate(DataLoad);
dvd.BeginInvoke(DataBind, dvd);
}
private DataView DataLoad()
{            
SqlConnection conn 
= new SqlConnection(@"Data Source=cat\sqlexpress;Initial Catalog=Northwind;Integrated Security=True");
SqlDataAdapter ada 
= new SqlDataAdapter("select * from orders ;waitfor delay '00:00:5';", conn);
DataTable dt 
= new DataTable();

conn.Open();
ada.Fill(dt);
conn.Close();

DataView dv 

= new DataView(dt);              
return dv;
 }
private void DataBind(IAsyncResult ar)
        {
            
if (ar == nullthrow new ArgumentNullException("ar");
            DataViewDelegate dvd1 
= ar.AsyncState as DataViewDelegate;
            System.Diagnostics.Trace.Assert(dvd1 
!= null"Invalid object type ");

            DataView dv 

= dvd1.EndInvoke(ar);
          
            VoidDelegate vd 
= delegate()
            {
                dataGridView1.DataSource 
= dv;
                
this.dataGridView1.Enabled = true;
                
this.progressBar1.Visible = false;
                
this.button1.Enabled = true;
            };
            dataGridView1.Invoke(vd);
//由主线程上的控件来invoke委托
            
        }

委托就是用线程来做异步的(个人看法,还没有从书本或网络中了解到)。
关于异步,下一篇是ASP.NET中做异步的例子,比起网络中现有例子更生动的几个做法。