0
我有一個函數叫做ChangeMap,它運行一個探路者並返回路徑(如果找到的話)。 通常我不會那麼用,但現在我需要檢查很多路徑,並且當我運行它們時,我的應用程序會凍結一會兒。返回任務結果
一條路徑需要70ms到800ms,並且應用程序不凍結,我想在任務中執行探路者部分並等待返回的路徑。
比方說,我有這個
public Path GetPath(int from, int to)
{
// Pathfinder work
return new Path(thePath);
}
我試過,但它不工作..
private Path GetPath2(int from, int to)
{
return Task.Run(() =>
{
return GetPath(from, to);
}).Result;
}
如果我試試這個,它給我的錯誤不能等待路徑
Path tempPath = await GetPath2(0, 10);
任何人都知道如何正確地做到這一點? 如何等待探路者的返回值,然後繼續,而不凍結整個應用程序。知道我有一大堆的功能,所以我不能把所有的東西在一個新的線程:/
'GetPath2'必須是'私人異步任務 GetPath2'。此外,還有一個約定用「Async」後綴異步方法。 –
zerkms
此外,值得一讀:https://blogs.msdn.microsoft.com/pfxteam/2012/03/24/should-i-expose-asynchronous-wrappers-for-synchronous-methods/ – zerkms
應用程序仍凍結與:/ – Haytam