NET 4.5
The recommended way in .NET 4.5 is to use Task.FromResult, Task.Run or Task.Factory.StartNew:
--FromResult:
public async Task DoWork()
{
int res = await Task.FromResult
}
private int GetSum(int a, int b)
{
return a + b;
}
Please check out Stefan’s comments on the usage of FromResult in the comments section below the post.
--Task.Run:
public async Task DoWork()
{
Func
int res = await Task.Run
}
private int GetSum(int a, int b)
{
return a + b;
}
--Task.Factory.StartNew:
public async Task DoWork()
{
Func
int res = await Task.Factory.StartNew
}
private int GetSum(int a, int b)
{
return a + b;
}
=========================== My Code ===================
Action actone;
Func
Action
private async void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
int posX = e.X;
int posY = e.Y;
actone = ActMethod;
funcone = ActMethod3;
acttwo = ActMethod2;
await Task.Factory.StartNew(() => actone());
await Task.Factory.StartNew(actone);
await Task.Factory.StartNew(() => acttwo(posX, posY));
string reu = await Task.FromResult
string result = await Task.Run(() => funcone(posX, posY));
}
void ActMethod()
{
Console.WriteLine("done");
}
void ActMethod2(int posX, int posY)
{
Console.WriteLine("done");
}
string ActMethod3(int posX, int posY)
{
Console.WriteLine("done");
return "func done";
}
댓글 없음:
댓글 쓰기