使用这个定义的方法:
async Task<TResult> RunLoadAsync(/* args */) {
var dateRange = new DateRange(date, date.AddDays(1));
var extracter = new ConversionsExtracter(dateRange, AdvertiserId);
var loader = new ConversionsLoader();
var intermediate = await Task.Factory.StartNew(
() => extracter.Start(), TaskCreationOptions.LongRunning
);
return await Task.Factory.StartNew(
() => loader.Start(intermediate), TaskCreationOptions.LongRunning
);
}
像这样调用它:
var result = await RunLoadAsync(/* parameters */);
更新:这是一个更完整的用法示例:
private async void PanelBoard_MouseCtlClick(object sender, HexEventArgs e) {
GoalHex = e.Coords;
// other processing to prepare.
try { Path = await MapBoard.GetDirectedPathAsync(MapBoard[StartHex], MapBoard[GoalHex]); }
catch (OperationCanceledException) { Path = default(IDirectedPath); }
}
public static async Task<IDirectedPath> GetDirectedPathAsync(
this IBoard<IHex> @this,
IHex start, IHex goal
) {
if (@this == null) throw new ArgumentNullException("this");
return @this.GetDirectedPath(start, goal);
}
IDirectedPath Path {
set { /* code here to refresh display when Path is ready */ }
}
public static Task<IDirectedPath> GetDirectedPathAsync(
this IBoard<IHex> @this,
IHex start, IHex goal
) {
if (@this == null) throw new ArgumentNullException("this");
return Task.Run<IDirectedPath>(
() => @this.GetDirectedPath(start, goal)
);
}
方法GetDirectedPathAsync()上的async限定符使其成为await的对象。