using System;
using System.Threading.Tasks;
namespace pc.asyncAwaitSimple00
{
class DataService
{
static Random rnd = new Random(DateTime.Now.Millisecond);
public void
UsingTask()
{
Console.WriteLine("Started using Task");
Task.Delay(rnd.Next(1000, 5000)).ContinueWith(
tr => Console.WriteLine("Ran using Task"));
}
public async void UsingAsyncAwait()
{
Console.WriteLine("started using async/await");
await Task.Delay(rnd.Next(1000,
5000));
Console.WriteLine("Ran using async/await");
}
}
class Program
{
static void Main(string[] args)
{
int option = -1;
var ds = new DataService();
do{
switch (option){
case 1: ds.UsingTask(); break;
case 2: ds.UsingAsyncAwait(); break;
}
Console.WriteLine("\n0)Exit\n1)Task\n2)async/await");
} while (int.TryParse(Console.ReadKey().KeyChar.ToString(), out option) && option != 0);
}
}
}
let's take a look at a slightly more involved example, this time our fetch data functions will return values.
using System;
using System.Threading.Tasks;
namespace pc.asyncAwaitSimpleExample
{
class DataService {
static Random rnd = new Random(DateTime.Now.Millisecond);
public Task<string> GetData1() {
return Task.Run(()=>getData()).ContinueWith(tr
=> {
Console.WriteLine(tr.Result);
return tr.Result;
});
}
string getData() {
Task.WaitAll(Task.Delay(rnd.Next(1000,
5000)));
return "my data 1";
}
public Task<string> GetData2() {
return Task.Run(()
=> {
Task.WaitAll(Task.Delay(rnd.Next(1000,
5000)));
return "my Data 2";
}).ContinueWith(tr => {
Console.WriteLine(tr.Result);
return tr.Result;
});
}
public async Task<string> GetData3() {
await Task.Delay(rnd.Next(1000,
5000));
string result = await Task.Run(() =>"my Data 3");
Console.WriteLine(result);
return result;
}
}
class Program {
static void Main(string[] args) {
int option = -1;
var ds = new DataService();
Console.WriteLine("\n0) Exit\n1)Task 1\n2)Task
2\n3)Async/await ");
do {
switch (option){
case 1: ds.GetData1(); break;
case 2: ds.GetData2(); break;
case 3: ds.GetData3(); break;
}
} while (int.TryParse(Console.ReadKey().KeyChar.ToString(), out option) && option != 0);
}
}
}
using System;
using System.Threading.Tasks;
namespace pc.asyncAwaitSimpleExample
{
class DataService {
static Random rnd = new Random(DateTime.Now.Millisecond);
public Task<string> GetData1() {
return Task.Run(()
=> getData());
}
string getData() {
Task.WaitAll(Task.Delay(rnd.Next(1000,
5000)));
return "my data 1";
}
public Task<string> GetData2() {
return Task.Run(()
=> {
Task.WaitAll(Task.Delay(rnd.Next(1000,
5000)));
return "my Data 2";
});
}
public async Task<string> GetData3() {
await Task.Delay(rnd.Next(1000,
5000));
return await Task.Run(() =>"my Data 3");
}
}
class Program {
static void Main(string[] args) {
int option = -1;
var ds = new DataService();
Console.WriteLine("\n0) Exit\n1)Task 1\n2)Task
2\n3)Async/await ");
do {
switch (option){
case 1:
Task.Run(() => Console.WriteLine(ds.GetData1().Result));
break;
case 2:
Task.Run(() => Console.WriteLine(ds.GetData2().Result));
break;
case 3:
Task.Run(() => Console.WriteLine(ds.GetData3().Result));
break;
}
} while (int.TryParse(Console.ReadKey().KeyChar.ToString(), out option) && option != 0);
}
}
}
Let's take a look at the following Task implemented function
public void GetData()
{
Task.Run(() => {
Thread.Sleep(1000);
return "My Data";
}).ContinueWith(
tr => {
Console.WriteLine(tr.Result);
});
}
public async void GetDataAsync()
{
var data = await Task.Run(() => {
Thread.Sleep(1000);
return "my data";
});
Console.WriteLine(data);
}