public class MegaClass
{
public async void StartAsync()
{
await Task.Run(() => { LongProcedure(); });
Console.WriteLine("End");
}
public void Start()
{
LongProcedure();
Console.WriteLine("End");
}
private void LongProcedure()
{
Thread.Sleep(3000);
}
public async Task MyMethodAsync()
{
int x = await LongRunningOperationAsync();
Console.WriteLine(x);
}
public async Task LongRunningOperationAsync()
{
await Task.Delay(3000);
return 1;
}
public async Task LongRunningOperationAsync(int _param)
{
await Task.Delay(3000);
return _param;
}
}
class Program
{
static void Main(string[] args)
{
var _c = new MegaClass();
//1 Sync
//_c.Start();
//2 Async
//c.StartAsync();
//3 Start and return task
//Task t = _c.MyMethodAsync();
//t.Wait();
//4
//Task task = new Task(_c.StartAsync);
//task.Start();
//5 GetResult
//Task x = _c.LongRunningOperationAsync();
//x.Wait();
//Console.WriteLine(x.Result);
//6 with param
//Task x = _c.LongRunningOperationAsync(3);
//x.Wait();
//Console.WriteLine(x.Result);
Console.WriteLine("Wait");
Console.ReadLine();
}
}
async await c# simple example
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: only a member of this blog may post a comment.