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<> 封装一个具有 ...
随机推荐
- 使用批处理文件命令行方式快速启动和停止IIS、SqlServer
原文:使用批处理文件命令行方式快速启动和停止IIS.SqlServer 虽然现在内存便宜了,但是自己还是嫌自己的512M内存太小,没办法,后台运行的东西太多了,有很多都是有用的没法关闭的.IIS和SQ ...
- Ubuntu升级后apache所有的失败,以解决虚拟文件夹的设置
问题描述: 将Ubuntu离12.04升级到14.04后,出现apache配置的虚拟文件夹所有失效.所有站点域名所有定向到根文件夹.无法分别訪问! 尝试方法: 開始以为是升级后Apache的问题.已经 ...
- Ansible@一个有效的配置管理工具--Ansible configure management--翻译(十)
未经书面许可,.请勿转载 Custom Modules Until now we have been working solely with the tools provided to us by A ...
- eclipse字母大写和小写转换的快捷键
大写转换小写 ctrl+shift+y 小写转换大写 ctrl+shift+x
- 接收终端Request.InputStream阅读
接收终端Request.InputStream阅读请求页面参数,最后字符串. byte[] byts = new byte[HttpContext.Current.Request.InputStrea ...
- 私人定制javascript事件处理机制(浅谈)
看到园子里关于事件监听发表的文章,我都有点不好意思写了.不过想想我的题目以私人定制作开头也就妥妥地写吧. 事件相关概念 1.事件类型 发生事件的字符串 有传统事件类型 比如表单.window事件等 D ...
- MapReduce的InputFormat学习过程
昨天,经过几个小时的学习.该MapReduce学习的某一位的方法的第一阶段.即当大多数文件的开头的Data至key-value制图.那是,InputFormat的过程.虽说过程不是非常难,可是也存在非 ...
- bootstrap collapse MVC .net漂亮的折叠List
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta na ...
- python解析命令行
可以解析这样的命令 ./cron_ctrl jobname1 --stop ;./cron_ctrl jobname1 --start;./cron_ctrl jobname1 --list #!/u ...
- C# foreach 有用方法具体解释
网上查资料,说foreach 不能改动迭代变量,仅仅能訪问迭代变量.自己理解也不是非常深,通过几个代码进行验证,发现foreach的使用方法还有点特别 验证方法: 1. 迭代变量 为int int[] ...