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

推荐订阅源

博客园 - 司徒正美
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
美团技术团队
WordPress大学
WordPress大学
罗磊的独立博客
Last Week in AI
Last Week in AI
人人都是产品经理
人人都是产品经理
爱范儿
爱范儿
MongoDB | Blog
MongoDB | Blog
J
Java Code Geeks
H
Hackread – Cybersecurity News, Data Breaches, AI and More
H
Help Net Security
S
SegmentFault 最新的问题
C
Check Point Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
腾讯CDC
Engineering at Meta
Engineering at Meta
The GitHub Blog
The GitHub Blog
F
Fortinet All Blogs
D
DataBreaches.Net
雷峰网
雷峰网
GbyAI
GbyAI
宝玉的分享
宝玉的分享

博客园 - 唐朝程序员

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