// 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 界面交互类库转的更多相关文章

  1. 推荐一个 JavaScript 日期处理类库 Moment.js

    官网: http://momentjs.com/ 处理时间的展示,很方便. 安装 bower install moment --save # bower npm install moment --sa ...

  2. Linux界面交互与目录结构

    一.交互通道 Linux系统环境默认有六个命令交互通道和一个图形界面交互通道,默认进入的是图形界面通道. 命令交互模式切换:ctrl+alt+F1-F6 图形界面交互模式:ctrl+alt+F7   ...

  3. cefsharp webBrowser Javascript 打开winForm界面

    在Cef webBrowser中,如果要调用 Javascript打开 一个 winForm界面,则需要使用  ShowDialog(),而不能使用 Show(),否则界面上的鼠标会转个不停

  4. HTA全解析:给VBS和JS一个强大的交互界面

    1.概述 HTA全称Html Application,在Windows中由mshta.exe加载执行,是一种本地程序.可使用html元素来构建界面,用JS或VBS来构建执行逻辑,从而强化Windows ...

  5. 在类库或winform项目中打开另一个winform项目的窗体

    假设类库或winform项目为A,另一个winform项目为B.那麽在A中添加一个接口,里面有一个Show方法,然后在B中写一个类b继承这个接口,并重写这个方法,具体内容为弹出某个窗体.然后在A中另一 ...

  6. winform设计一个登录界面和修改密码的界面-自动切换窗体(问题[已解] 望一起讨论)(技术改变世界-cnblog)

    http://www.cnblogs.com/IAmBetter/archive/2012/01/14/2322156.html winform设计一个登录界面和修改密码的界面-自动切换窗体(问题[已 ...

  7. C# winform 界面美化技巧(扁平化设计)

    关于C#界面美化的一些小技巧 在不使用第三方控件如 IrisSkin 的前提下,依然可以对winform做出让人眼前一亮的美化 首先,我们先来实现主界面的扁平化 此处分为两个步骤,第一步是更改winf ...

  8. 循序渐进开发WinForm项目(4)--Winform界面模块的集成使用

    随笔背景:在很多时候,很多入门不久的朋友都会问我:我是从其他语言转到C#开发的,有没有一些基础性的资料给我们学习学习呢,你的框架感觉一下太大了,希望有个循序渐进的教程或者视频来学习就好了. 其实也许我 ...

  9. winform界面特效470多例

    一共470多例winform 界面特效的源码. 实例030 窗口颜色的渐变 实例说明 在程序设计时,可以通过设置窗体的BackColor属性来改变窗口的背景颜色.但是这个属性改变后整个窗体的客户区都会 ...

随机推荐

  1. php学习笔记

    header("Content-Type:text/html;charset=utf-8");//设置中文请求中文 $host = "localhost";// ...

  2. MapReduce工作原理讲解

    第一部分:MapReduce工作原理 MapReduce 角色•Client :作业提交发起者.•JobTracker: 初始化作业,分配作业,与TaskTracker通信,协调整个作业.•TaskT ...

  3. autolayout

    autolayout.因为之前都是用frame,用代码来做,并且在布局时也很少用storyboard和xib.使得我再这方便经验很欠缺,想用,但是又怕用不好,出现各种意想不到的bug.但是又忽然想到, ...

  4. Nuget版本冲突的问题

    有两个类库项目,一个引用了比如Newtonsoft.Json 6.0, 另一个引用了比如Newtonsoft.Json 8.0, 然后另一个exe项目同时引用了这两个类库项目. 那么在编译的时候会报w ...

  5. SQL Server 的数据表简单操作

    --创建数据表--[use 要创建数据表的数据库名称go]create table 要创建的表名(字段名 数据类型[长度] [null | not null] [primary key],... .. ...

  6. 在sql server中利用with as实现递归功能

    在sqlserver2005之前,要实现递归功能比较麻烦,比如可能会要用到临时表与while语句来循环.自sqlserver2005之后,新增了with as功能语法,即 公用表达式(CTE),让递归 ...

  7. 带隙基准(Bandgap,BG)

    Bandgap voltage reference,常常有人简单地称它为Bandgap.最经典的带隙基准是利用一个与温度成正比的电压与一个与温度成反比的电压之和,二者温度系数相互抵消,实现与温度无关的 ...

  8. 文本换行word-wrap、word-break与white-space

    本文同步至微信公众号:http://mp.weixin.qq.com/s?__biz=MzAxMzgwNDU3Mg==&mid=401671055&idx=1&sn=b88c9 ...

  9. netty发送utf-8编码的信息

    /** * */ @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { // (1) // ...

  10. ViewPager如下效果你研究过吗?

    1:ViewPager实现欢迎页面动画效果 ViewPager实现欢迎页面动画滑动切换view效果,页面切换添加优美的动画, //主要代码实现 public void animateSecondScr ...