//filename: MathOperations.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace TestAsyncAwait
{
public class MathOperations
{
public static double MultiplyByTwo(double d)
{
return d * 2;
} public static double Square(double d)
{
return d * d;
} }
} //filename: MyClass.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks; namespace TestAsyncAwait
{
public class MyClass
{
public MyClass()
{
//DisplayValue();
DisplayValueWithContinuationTask();
Console.WriteLine("MyClass() end.");
} public Task<double> GetValueAsync(double num1, double num2)
{
return Task.Run<double>(() => {
Thread.Sleep(1000);
return num1 + num2;
});
} public async void DisplayValue()
{
double result = await GetValueAsync(1, 2);
Console.WriteLine("result is :" + result);
} public void DisplayValueWithContinuationTask()
{
Task<double> task = GetValueAsync(1, 2);
task.ContinueWith(t =>
{
double result = t.Result;
Console.WriteLine("result is :" + result);
});
} }
} //filename: Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks; namespace TestAsyncAwait
{
class Program
{
public static Tuple<int, int> Divide(int dividend, int divisor)
{
int result = dividend / divisor;
int reminder = dividend % divisor;
return Tuple.Create<int, int>(result, reminder);
} private delegate string GetAString(); static void ProcessAndDispalyNumber(Func<double,double> action, double value){
double result = action(value);
Console.WriteLine(string.Format("value={0},result={1}", value, result));
} #region 异步编程实验 static string Greeting(string name)
{
Thread.Sleep(3000);
return string.Format("threadID:[{0}] says: Hello, {1}!", Thread.CurrentThread.ManagedThreadId, name);
} static Task<string> GreetingAsync(string name)
{
return Task.Run<string>(() => {
return Greeting(name);
});
} async static void CallerWithAsync()
{
string result = await GreetingAsync("张三");
Console.WriteLine(result);
} static void CallerWithContinuationWith()
{
Task<string> task = GreetingAsync("张三");
task.ContinueWith(t => {
string result = t.Result;
Console.WriteLine(result);
});
} async static void MultipleAsyncMethods()
{
string result1 = await GreetingAsync("张三@");
string result2 = await GreetingAsync("李四@");
Console.WriteLine("Finished with 2 result: {0},{1}", result1, result2);
} async static void MultipleAsyncMethodsWithCombinators1()
{
Task<string> t1 = GreetingAsync("张三1");
Task<string> t2 = GreetingAsync("李四1");
await Task.WhenAll(t1, t2);
Console.WriteLine("Finished with 2 result: {0},{1}", t1.Result, t2.Result);
} async static void MultipleAsyncMethodsWithCombinators2()
{
Task<string> t1 = GreetingAsync("张三2");
Task<string> t2 = GreetingAsync("李四2");
string[] result = await Task.WhenAll(t1, t2);
Console.WriteLine("Finished with 2 result: {0},{1}", result[0], result[1]);
} #endregion static void Main(string[] args)
{
/*
CallerWithAsync();
CallerWithContinuationWith();
MultipleAsyncMethods();
MultipleAsyncMethodsWithCombinators1();
MultipleAsyncMethodsWithCombinators2(); Console.WriteLine(Thread.CurrentThread.ManagedThreadId + ": Done.");
*/
ParallelLoopResult result = Parallel.For(0, 10, async i => {
Console.WriteLine("{0}, task: {1}, thread: {2}", i, Task.CurrentId,
Thread.CurrentThread.ManagedThreadId);
//Thread.Sleep(10);
await Task.Delay(10);
Console.WriteLine("{0}, task: {1}, thread: {2}", i, Task.CurrentId,
Thread.CurrentThread.ManagedThreadId);
});
Console.WriteLine("Is completed: {0}", result.IsCompleted); /*
MyClass mc = new MyClass(); Console.WriteLine("-----------------"); var result = Divide(5, 2);
Console.WriteLine(string.Format("result={0},reminder={1}",result.Item1, result.Item2)); int x = 4;
//GetAString func = new GetAString(x.ToString);
GetAString func = x.ToString;
Console.WriteLine(func());
Console.WriteLine(func.Invoke()); Func<double, double>[] operations =
{
MathOperations.MultiplyByTwo,
MathOperations.Square
};
foreach (var item in operations)
{
ProcessAndDispalyNumber(item, 3.14);
} string midPart = ", middle part,";
Func<string, string> anonDel = delegate(string value) {
string s = value + midPart;
s += " last part";
return s;
};
Console.WriteLine(anonDel("Hello")); Func<string, string> anonDel2 = (string value) =>
{
string s = value + midPart;
s += " last part. version 2";
return s;
};
Console.WriteLine(anonDel2("Hello")); Func<string, string> anonDel3 = value =>
{
string s = value + midPart;
s += " last part. version 3";
return s;
};
Console.WriteLine(anonDel3("Hello")); int someValue = 5;
Func<int, int> f = p1 => p1 + someValue;
Console.WriteLine(f(1));
someValue = 6;
Console.WriteLine(f(1));
//var lists = new List<string>() { "1","2"}; dynamic dyn;
dyn = 100;
Console.WriteLine(dyn.GetType());
Console.WriteLine(dyn); dyn = "abc中国";
Console.WriteLine(dyn.GetType());
Console.WriteLine(dyn);
*/
Console.ReadKey();
}
}
}

C#5.0 .net 4.5示例的更多相关文章

  1. .Net Core 1.0.0 RC2安装及示例教程

    前几天微软发布了.Net Core1.0.0 RC2 Preview版本,一直都想尝试下跨平台的.Net Core,一直拖到今天,也参考了下园友们的经验,闲时整理了一下安装的步骤,供大家参考. 我们要 ...

  2. RSS介绍、RSS 2.0规范说明和示例代码

    RSS是一种消息来源格式规范,用以发布经常更新资料的网站,例如博客.新闻的网摘.RSS文件,又称做摘要.网摘.更新.频道等,包含了全文或节选文字,再加上一定的属性数据.RSS让发布者自动发布信息,也使 ...

  3. OAuth 2.0 Server PHP实现示例

    需求实现三方OAuth2.0授权登录 使用OAuth服务OAuth 2.0 Server PHP 环境nginx mysqlphp 框架Yii 一 安装 项目目录下安装应用 composer.phar ...

  4. kafka producer 0.8.2.1 示例

    package test_kafka; import java.util.Properties; import java.util.concurrent.atomic.AtomicInteger; i ...

  5. kafka consumer 0.8.2.1示例代码

    package test_kafka; import java.util.ArrayList; import java.util.HashMap; import java.util.List; imp ...

  6. SkylineGlobe 7.0版本 矢量数据查询示例代码

    在Pro7.0.0和7.0.1环境下测试可用. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...

  7. 微信OAuth2.0网页授权php示例

    1.配置授权回调页面域名,如 www.aaa.com 2.模拟公众号的第三方网页,fn_system.php <?php if(empty($_SESSION['user'])){ header ...

  8. 部署Bookinfo示例程序详细过程和步骤(基于Kubernetes集群+Istio v1.0)

    部署Bookinfo示例程序详细过程和步骤(基于Kubernetes集群+Istio v1.0) 部署Bookinfo示例程序   在下载的Istio安装包的samples目录中包含了示例应用程序. ...

  9. C#7.0中有哪些新特性?

    以下将是 C# 7.0 中所有计划的语言特性的描述.随着 Visual Studio “15” Preview 4 版本的发布,这些特性中的大部分将活跃起来.现在是时候来展示这些特性,你也告诉借此告诉 ...

随机推荐

  1. 【python游戏编程之旅】第四篇---pygame中加载位图与常用的数学函数。

    本系列博客介绍以python+pygame库进行小游戏的开发.有写的不对之处还望各位海涵. 在上一篇博客中,我们学习了pygame事件与设备轮询.http://www.cnblogs.com/msxh ...

  2. CodeForces Round 196

    Div2-A 题意:有m个拼图,每个拼图有f[i]块.从中选出n个,使得 (其中块数最大减块数最小的值) 最小.思路:把f按从小到大的顺序排序,然后顺次尝试. #include<stdio.h& ...

  3. BZOJ4346 : [POI2016]Nadajniki

    设$f[x][j]$表示$x$点不放无线,它的儿子里放了$j$个无线,且对$x$的父亲不作要求时的最小代价. $g[x][j]$表示$x$点不放无线,要求$x$的父亲至少放$j$个无线时的最小代价. ...

  4. 首发 手把手教你制作 Windows8 应用程序内部的 hubtile (动态瓷砖控件) MetroStyle(转)

    http://blog.csdn.net/wangrenzhu2011/article/details/8175492 (转) 在metro 风格中 动态磁贴是他的精髓 在wp7 的开发中 我们可以使 ...

  5. 【TYVJ】1338 QQ农场(最大流+最大权闭合图)

    http://tyvj.cn/Problem_Show.aspx?id=1338 时间才排到rank7,还不快啊囧.isap我常数都写得那么小了... 最大权闭合图看我另一篇博文吧 此题很明显的模型. ...

  6. Codeforces Round #195 (Div. 2) D题Vasily the Bear and Beautiful Strings

    这场CF,脑子乱死啊...C题,搞了很长时间,结束了,才想到怎么做.B题,没看,D题,今天看了一下,很不错的组合题. 如果n和m都挺多的时候 以下情况都是变为1,根据偶数个0,最后将会为1,奇数个0, ...

  7. MySQL添加字段和修改字段的方法

    添加表字段 alter table table1 add transactor varchar(10) not Null; alter table   table1 add id int unsign ...

  8. Vim 学习资料

    VIM中文手册 简明 Vim 练级攻略 所需即所获:像 IDE 一样使用 vim

  9. php页面之间传值

    echo("<script>window.open('2.php?head=".$head."');<script>");

  10. include动作标记和include指令标记学习笔记

    我的jsp学习参考书是耿祥义,张跃平编著的jsp大学使用教程这本书,我也向大家推荐这本书,我觉得这本书适合我的学习方式,知识的讲解透彻易懂. include指令标记                   ...