Lamda Action Func Thread 实例
lamda表达式
格式:( 形参列表 ) => { 函数体 }
作用:简化匿名方法的书写,可用在任何可使用匿名方法和强类型代理的地方;
Action是无返回值的泛型委托。
Action 表示无参,无返回值的委托
Action<int,string> 表示有传入参数int,string无返回值的委托
Action<int,string,bool> 表示有传入参数int,string,bool无返回值的委托
Action<int,int,int,int> 表示有传入4个int型参数,无返回值的委托
Action至少0个参数,至多16个参数,无返回值。
Func是有返回值的泛型委托
Func<int> 表示无参,返回值为int的委托
Func<object,string,int> 表示传入参数为object, string 返回值为int的委托
Func<object,string,int> 表示传入参数为object, string 返回值为int的委托
Func<T1,T2,,T3,int> 表示传入参数为T1,T2,,T3(泛型)返回值为int的委托
Func至少0个参数,至多16个参数,根据返回值泛型返回。必须有返回值,不可void
实例:
通过Acton 分装用户方法,以两种更新界面UI (源代码)
private void btnFunc_Click(object sender, EventArgs e)
{
var n = TestFunc((N, Q) =>
{
return N * Q;
}, , ); txtResult.Text = "Func-结果是:" + n;
} private void btnAction_Click(object sender, EventArgs e)
{
TestAtction((N, Q) =>
{
txtResult.Text = "Action-结果" + (N * Q).ToString();
}, , );
} private void btnThrend_Click(object sender, EventArgs e)
{
txtResult.Text = string.Empty;
threadStatus = true;
new Thread(() =>
{
Dothread((n) =>
{
this.Invoke(new Action(() =>
{
txtResult.Text += string.Format("现在是:{0},第{1}次执行", DateTime.Now.ToString(), n.ToString()) + "\r\n";
}));
});
}).Start();
} private void btnthreaddelegate_Click(object sender, EventArgs e)
{
txtResult.Text = string.Empty;
threadStatus = true;
new Thread(() =>
{
Dothread((n) =>
{
WriteInvoke(string.Format("现在是:{0},第{1}次执行", DateTime.Now.ToString(), n.ToString()) + "\r\n");
});
}).Start();
} public void TestAtction(Action<int, int> ac, int n, int q)
{
ac(n, q);
} public int TestFunc(Func<int, int, int> func, int a, int b)
{
return func(a, b);
} private static bool threadStatus; #region "线程UI委托"
delegate void WriteInvokeCallBack(string msg);
void WriteInvoke(string msg)
{
if (this.InvokeRequired)
{
WriteInvokeCallBack Wc = new WriteInvokeCallBack(WriteInvoke);
this.Invoke(Wc, msg);
}
else
{
txtResult.Text += msg;
}
}
#endregion private void Dothread(Action<int> ac)
{
lock (this)
{
int i = ;
while (threadStatus)
{
i += ;
ac(i);
if (i > ) threadStatus = false;
Thread.Sleep();
}
}
}
Lamda Action Func Thread 实例的更多相关文章
- VS2012 Unit Test(Void, Action, Func) —— 对无返回值、使用Action或Func作为参数、多重载的方法进行单元测试
[提示] 1. 阅读文本前希望您具备如下知识:了解单元测试,了解Dynamic,熟悉泛型(协变与逆变)和Lambda,熟悉.NET Framework提供的 Action与Func委托.2.如果您对单 ...
- 关于struts和Spring 结合到一起之后存在ACtion创建单实例还是多
struts 2的Action是多实例的并非单例,也就是每次请求产生一个Action的对象.原因是:struts 2的Action中包含数据,例如你在页面填写的数据就会包含在Action的成员变量里面 ...
- (C#) Action, Func, Predicate 等泛型委托
(转载网络文章) (1). delegate delegate我们常用到的一种声明 Delegate至少0个参数,至多32个参数,可以无返回值,也可以指定返回值类型. 例:public del ...
- Lambda Action Func练习
namespace lambda { delegate void TestDelegate(string s); class Program { static void Main(string[] a ...
- Delegate,Action,Func,匿名方法,匿名委托,事件 (转载)
Delegate,Action,Func,匿名方法,匿名委托,事件 (转载) 一.委托Delegate 一般的方法(Method)中,我们的参数总是string,int,DateTime...这些基本 ...
- c# Action,Func,Predicate委托
System命名空间下已经预先定义好了三中泛型委托,Action,Func和Predicate,这样我们在编程的时候,就不必要自己去定义这些委托了 Action是没有返回值的 Func是带返回值的 不 ...
- Delegate,Action,Func,Predicate的使用与区别
C#4.0推出后,类似Linq,Lamda表达式等许多新的程序写法层次不穷.与之相关的Delegate,Action,Func,Predicate的使用和区别也常常让大家迷惑,此处就结合实际的应用,对 ...
- C# 委托应用总结(委托,Delegate,Action,Func,predicate)
C# 委托应用总结 一.什么是委托 1.1官方解释 委托是一种定义方法签名的类型.当实例化委托时,您可以将其实例与任何具有兼容签名的方法相关联.您可以通过委托实例调用方法. 1.2个人理解 委托就是执 ...
- winform总结2> Action<> ,Action,func<>,委托相关的理解
1,他们是什么: Action 封装一个方法,该方法不具有参数并且不返回值. Action<> 封装一个方法,该方法具有最多16个参数并且不返回值. func<> 封装一个具有 ...
随机推荐
- ElasticSearch实战
ElasticSearch实战-入门 1.概述 今天接着<ElasticSearch实战-日志监控平台>一文来给大家分享后续的学习,在<ElasticSearch实战-日志监控平台& ...
- JS数据绑定模板artTemplate试用
之前写JS绑定数据曾经用过tmpl库,虽然功能比较强大但是感觉不是很轻量,对于相对简单的数据需求显得有些臃肿.而Ajax返回数据自己拼接html的方式又显得不够高端,因此今天看了一篇介绍artTemp ...
- django 简易博客开发 1 安装、创建、配置、admin使用(转)
Django 自称是“最适合开发有限期的完美WEB框架”.本文参考<Django web开发指南>,快速搭建一个blog 出来,在中间涉及诸多知识点,这里不会详细说明,如果你是第一次接触D ...
- Apriori算法Python实现
Apriori如果数据挖掘算法的头发模式挖掘鼻祖,从60年代开始流行,该算法非常简单朴素的思维.首先挖掘长度1频繁模式,然后k=2 这些频繁模式的长度合并k频繁模式.计算它们的频繁的数目,并确保其充分 ...
- EJBCA于Linux安装在
于windows为了测试安装,装在linuxserver因为CN使用ip需要重新加载.....再折腾.这里有一些地方需要注意 一.所需文件 内容准备不说,请参阅我在以前的文章<EJBCA于win ...
- LwIP学习笔记——STM32 ENC28J60移植与入门
0.前言 去年(2013年)的整理了LwIP相关代码,并在STM32上"裸奔"成功.一直没有时间深入整理,在这里借博文整理总结.LwIP的移植过程细节很多,博文也不可能一一 ...
- 基于STM32旋转编码器
..\..\SYSTEM\usart\usart.c(1): error: #5: cannot open source input file "sys.h": No such ...
- 阅读INI档 - Delphi一片
程序往往需要读一些用户设置值.如何完成这一过程? B/S程序一般使用XML档.和C/S程序使用INI档. 前篇<C#迁移之callXBFLibrary - 2(调用非托管DLL)>是C#读 ...
- 多层次的Json字符串转化为对象
using Arvato.CRM.DataTrans.ConsoleHost.Model;using System;using System.Collections.Generic;using Sys ...
- crawler_jsoup HTML解析器_使用选择器语法来查找元素
参照:http://www.open-open.com/jsoup/selector-syntax.htm 使用选择器语法来查找元素 问题 你想使用类似于CSS或jQuery的语法来查找和操作元素. ...