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

推荐订阅源

Spread Privacy
Spread Privacy
L
LangChain Blog
爱范儿
爱范儿
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Google DeepMind News
Google DeepMind News
有赞技术团队
有赞技术团队
博客园 - 【当耐特】
人人都是产品经理
人人都是产品经理
H
Hackread – Cybersecurity News, Data Breaches, AI and More
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Engineering at Meta
Engineering at Meta
P
Privacy International News Feed
I
Intezer
NISL@THU
NISL@THU
Jina AI
Jina AI
G
GRAHAM CLULEY
C
CERT Recently Published Vulnerability Notes
S
Schneier on Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cisco Talos Blog
Cisco Talos Blog
Scott Helme
Scott Helme
MyScale Blog
MyScale Blog
IT之家
IT之家
Security Latest
Security Latest
C
Cisco Blogs
Cyberwarzone
Cyberwarzone
aimingoo的专栏
aimingoo的专栏
V
Vulnerabilities – Threatpost
L
LINUX DO - 热门话题
Recorded Future
Recorded Future
The Hacker News
The Hacker News
C
CXSECURITY Database RSS Feed - CXSecurity.com
月光博客
月光博客
A
Arctic Wolf
云风的 BLOG
云风的 BLOG
N
Netflix TechBlog - Medium
K
Kaspersky official blog
S
Securelist
M
MIT News - Artificial intelligence
T
Threat Research - Cisco Blogs
P
Palo Alto Networks Blog
Simon Willison's Weblog
Simon Willison's Weblog
Know Your Adversary
Know Your Adversary
WordPress大学
WordPress大学
Project Zero
Project Zero
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
N
News and Events Feed by Topic
AWS News Blog
AWS News Blog
T
The Exploit Database - CXSecurity.com
T
The Blog of Author Tim Ferriss

博客园 - 唐朝程序员

DBCC SHOWCONTIG、DBCC DBREINDEX。 SQL Server2005索引碎片分析和解决方法 some things 微软自带的防反编译工具dotfuscator.exe的使用 在HttpModule中使用gzip,deflate协议对aspx页面进行压缩(转) SQL查询重复数据和清除重复数据 lucene 笔记 SqlServer的汉字转拼音码的函数 windows系统事件查询 2010考研冲刺必备 2010公务员考试必备资料 url重写适用html为伪静态后真实的html无法访问的解决方法 SQL的 优化 (某篇的精简版) SQL 优化 (某篇的精简版) U影资源网原域名被封了,现在叫狐库网,启用了新的域名 vs2008 下载地址以及正式版序列号 Windows XP和Office2003通过正版验证,免去黑屏之忧 NOKIA诺基亚PC套件在2003系统上的安装方法 在.net 2.0 中使用ftp
推荐一个winform 界面交互类库转
唐朝程序员 · 2014-11-18 · via 博客园 - 唐朝程序员
// Copyright (c) 2008 CodeToast.com and Nicholas Brookins
//This code is free to use in any application for any use if this notice is left intact.
//Just don't sue me if it gets you fired.  Enjoy!

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.Runtime.Remoting.Messaging;
using System.Threading;
using System.Windows.Forms;

namespace CodeToast {
    public static class Async {

        static Dictionary<string, object> methodLocks = new Dictionary<string, object>();

        #region Async 'Do' overloads, for ease of use
        /// <summary>
        /// Fires off your delegate asyncronously, using the threadpool or a full managed thread if needed. 
        /// This overload always tries the ThreadPool and DOES NOT check for reentrance.
        /// </summary>
        /// <param name="d">A delegate with a return value of some sort - can be cast to (DlgR) from an anonymous delgate with a return: Async.Do((DlgR)MyMethod);</param>
        /// <param name="getRetVal">If true, and the method/delgete returns something, it is included in the AsyncRes returned (after the method completes)</param>
        /// <returns>AsyncRes with all kind o' goodies for waiting, etc.</returns>
        public static AsyncRes Do(DlgR d, bool getRetVal) {
            return Do(d, getRetVal, ReenteranceMode.Allow);
        }

        /// <summary>
        /// Fires off your delegate asyncronously, using the threadpool or a full managed thread if needed. 
        /// This overload always tries the ThreadPool and DOES NOT check for reentrance.
        /// </summary>
        /// <param name="d">A void delegate - can be cast to (Dlg) from an anonymous delgate or method:  Async.Do((Dlg)MyVoidMethod)</param>
        /// <returns>AsyncRes with all kind o' goodies for waiting, etc.</returns>
        public static AsyncRes Do(Dlg d) {
            return Do(d, ReenteranceMode.Allow);
        }

        /// <summary>
        /// Fires off your delegate asyncronously, using the threadpool or a full managed thread if needed.
        /// </summary>
        /// <param name="d">A delegate with a return value of some sort - can be cast to (DlgR) from an anonymous delgate with a return: Async.Do((DlgR)MyMethod);</param>
        /// <param name="rMode">If true, will make sure no other instances are running your method.</param>
        /// <param name="getRetVal">If true, and the method/delgete returns something, it is included in the AsyncRes returned (after the method completes)</param>
        /// <returns>AsyncRes with all kind o' goodies for waiting, resturn and result values, etc.</returns>
        public static AsyncRes Do(DlgR d, bool getRetVal, ReenteranceMode rMode) {
            return Do(d, null, getRetVal, null, true, rMode, null, true);
        }

        /// <summary>
        /// Fires off your delegate asyncronously, using the threadpool or a full managed thread if needed.
        /// </summary>
        /// <param name="d">A void delegate - can be cast to (Dlg) from an anonymous delgate or method:  Async.Do((Dlg)MyVoidMethod);</param>

        /// <param name="rMode">If true, will make sure no other instances are running your method.</param>
        /// <returns>AsyncRes with all kind o' goodies for waiting, result values, etc.</returns>
        public static AsyncRes Do(Dlg d, ReenteranceMode rMode) {
            return Do(null, d, false, null, true, rMode, null, true);
        }

        /// <summary>
        /// Fires off your delegate asyncronously, using the threadpool or a full managed thread if needed.
        /// </summary>
        /// <param name="d">A delegate with a return value of some sort - can be cast to (DlgR) from an anonymous delgate with a return: Async.Do((DlgR)MyMethod);</param>
        /// <param name="state">A user object that can be tracked through the returned result</param>
        /// <param name="tryThreadPool">True to use the TP, otherwise just go to a ful lthread - good for long running tasks.</param>
        /// <param name="rMode">If true, will make sure no other instances are running your method.</param>
        /// <param name="getRetVal">If true, and the method/delgete returns something, it is included in the AsyncRes returned (after the method completes)</param>
        /// <returns>AsyncRes with all kind o' goodies for waiting, resturn and result values, etc.</returns>
        public static AsyncRes Do(DlgR d, bool getRetVal, object state, bool tryThreadPool, ReenteranceMode rMode) {
            return Do(d, null, getRetVal, state, tryThreadPool, rMode, null, true);
        }

        /// <summary>
        /// Fires off your delegate asyncronously, using the threadpool or a full managed thread if needed.
        /// </summary>
        /// <param name="d">A void delegate - can be cast to (Dlg) from an anonymous delgate or method:  Async.Do((Dlg)MyVoidMethod);</param>
        /// <param name="state">A user object that can be tracked through the returned result</param>
        /// <param name="tryThreadPool">True to use the TP, otherwise just go to a ful lthread - good for long running tasks.</param>
        /// <param name="rMode">If true, will make sure no other instances are running your method.</param>
        /// <returns>AsyncRes with all kind o' goodies for waiting, result values, etc.</returns>
        public static AsyncRes Do(Dlg d, object state, bool tryThreadPool, ReenteranceMode rMode) {
            return Do(null, d, false, state, tryThreadPool, rMode, null, true);
        }
        #endregion

        The Big Main private 'Do' method - called by all overloads.

        Before and after - helper methods

        UI Overloads
    }

    AsyncRes class

    Definitions of enums and delegates
}

 这个类库是在codeproject上发现的,主要是用于在.net 2.0,3.5的框架下,不可以在非创建线程下雨控件进行交互的限制,类库封装得比较好,使用了匿名方法。

 异步获取控件值:

AsyncRes result = Async.UI(
    //make sure the delegate/method returns a value:
    delegate { return textBox1.Text; },
    true, //yes, we want to get the return value
    myForm, //the control to invoke on
    null, //the state object, we don't need to track anything.
    true, //invoke asynchronously?
    ReenteranceMode.Allow); //don't worry about thread safety in this case.

// . do other things  //

// now make sure the task above has completed.. 
result.AsyncWaitHandle.WaitOne();

//and use the value
Console.WriteLine("The textbox says: " + result.ReturnValue);

  同步设置控件值: 

Async.UI(delegate { textBox1.Text = "This is way easier!"; }, textBox1, true);

  转自:http://www.cnblogs.com/perfectdesign/archive/2009/03/02/1401569.html