推荐一个winform 界面交互类库转

// 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

你可能感兴趣的:(WinForm)