// 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. 常用MySQL命令整理

    备份数据库 #.备份数据库到sql文件 mysqldump --add-drop-database -h localhost -uusername ppassword dbname > dbna ...

  2. 这是个简单的UTF8转码的小Demo

    NSString *name = @"你好啊"; NSString *string = [NSString stringWithFormat:@"%@",nam ...

  3. gtktree和gtktext使用时要在文件中定义GTK_ENABLE_BROKEN

    linux下调试程序,出现如下错误: /tmp/ccG8fpwg.o: In function `apache_viewlog_form':apache.c:(.text+0x776): undefi ...

  4. html显示时间

    <html> <head> <script type="text/javascript"> function time() { var time ...

  5. GsonUtils.getGson().fromJson() 转泛型集合

    List<QiTaFree> qiTaFreeList = GsonUtils.getGson().fromJson(exhiMain.getQiTaFressJson(), new Ty ...

  6. 线程+IO流

    第十八天知识点总结 线程的停止: 1.停止一个线程,一般是通过一个变量来控制. 2.如果需要停止一个处于一个等待状态的线程,那么需要配合interrupt方法来完成 守护线程(后台线程):在一个进程中 ...

  7. cocos2d-js去掉左下角的三行数字(帧数)

    project.json 里面"showFPS": true, 改成 false 就行了... 调整帧率也在这里调整 或者是 同cocos2dx,cocos2d-js左下角的FPS ...

  8. getGLES1ExtensionString: Could not find GLES 1.x config!

    编辑模拟器中  Emulated performance 中不选  auto 和  Hardware - GLES 2.0,选择Software-GLES2.0.解决问题

  9. Xrm.Utility.openEntityForm 时404.15 maxQueryString 错误 和 长度超过maxQueryStringLength值 错误

    最近的项目里用到Xrm.Utility.openEntityForm 创建新记录时分别碰到以下错误: 以及 这两个错误都是因为想传递给表单的参数太多导致的url 查询参数太长导致的,前者是因为iis的 ...

  10. Qt事件过滤器Event Filter

    事件过滤器针对一类或者多种不同类型的对象,定义了重写操作. 简单的说: 1.先对UI对象注册Event Filter 例如:    ui->drawBtn->installEventFil ...