// 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. qsort库函数的用法

    qsort 功 能: 使用快速排序例程进行排序  用 法: void qsort(void *base, int nelem, int width, int (*fcmp)(const void *, ...

  2. java 在linux环境下写入 syslog 问题研究

    1.Syslog 在Unix类操作系统上,syslog广泛应用于系统日志.syslog日志消息既可以记录在本地文件中,也可以通过网络发送到接收syslog的服务器.接收syslog的服务器可以对多个设 ...

  3. 控制input框不能更改里面的内容

    <input type="text" disabled="true"/> 这个是给input设置一个属性.控制它可以不能改变里面的内容.已经试过了! ...

  4. egret.Tween、egret.Ease

    循环调用.只能设置boolean,不能设置循环次数. egret.Tween.).call(()=>{ console.log("循环调用"); }) 每次改变时,调用onC ...

  5. prolog --寻找neni (2)

    混合查询 我们可以把简单的查询连接起来,组成复杂的查询. ?- location(X,kitchen),edible(X). 简单查询只有一个目标,而混合查询可以把这些目标连接起来.从而进行较为复杂的 ...

  6. 转:永久解决火狐浏览器出现的flash版本更新问题

    发现问题: 1.找到火狐浏览器,并打开出现问题的页面. 2.点击火狐浏览器的"打开菜单"按钮,选择"附加组件"按钮. 3.在"搜索所有添加附件&quo ...

  7. 安装XMind

    XMind是一款思维导图软件.可编辑整理头脑中的想法,以使其脉络更加清晰. 在学习知识过程中,用这个工具也不错. 官方网站:  http://www.xmind.net 其它版本: http://ww ...

  8. 1-12 ARP协议

    ARP(Address Resolution Protocol)地址解析协议,负责将相应的IP地址解析成MAC地址. 在局域网中,网络中实际传输的是‘帧’,帧里面包含了目的主机的MAC.ARP就是用来 ...

  9. 使用MLeaksFinder检测项目中的内存泄露

    github地址:https://github.com/Zepo/MLeaksFinder MLeaksFinder使用简单方便,可以帮助你在开发时发现内存泄露你的iOS应用.它可以自动发现和UIVi ...

  10. chapter1 渗透测试与metasploit

    网络对抗技术课程学习 chapter1 渗透测试与metasploit 一.读书笔记 二.渗透测试 通过模拟恶意攻击者的技术与方法进行攻击,挫败目标系统安全控制措施,取得访问控制权,并发现具备业务影响 ...