本文整理汇总了C#中TaskCompletionSource.TrySetResultAsync方法的典型用法代码示例。如果您正苦于以下问题:C# TaskCompletionSource.TrySetResultAsync方法的具体用法?C# TaskCompletionSource.TrySetResultAsync怎么用?C# TaskCompletionSource.TrySetResultAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TaskCompletionSource
的用法示例。
在下文中一共展示了TaskCompletionSource.TrySetResultAsync方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ScreenshotAsync
/// <summary>
/// Starts a task that waits for the next rendering from Chrome.
/// Chrome also renders the page loading, so if you want to see a complete rendering,
/// only start this task once your page is loaded (which you can detect via FrameLoadEnd
/// or your own heuristics based on evaluating JavaScript).
/// It is your responsibility to dispose the returned Bitmap.
/// The bitmap size is determined by the Size property set earlier.
/// </summary>
/// <param name="ignoreExistingScreenshot">Ignore existing bitmap (if any) and return the next avaliable bitmap</param>
/// /// <param name="blend">Choose which bitmap to retrieve, choose <see cref="PopupBlending.Blend"/> for a merged bitmap.</param>
/// <returns>Task<Bitmap>.</returns>
public Task<Bitmap> ScreenshotAsync(bool ignoreExistingScreenshot = false, PopupBlending blend = PopupBlending.Main)
{
// Try our luck and see if there is already a screenshot, to save us creating a new thread for nothing.
var screenshot = ScreenshotOrNull(blend);
var completionSource = new TaskCompletionSource<Bitmap>();
if (screenshot == null || ignoreExistingScreenshot)
{
EventHandler newScreenshot = null; // otherwise we cannot reference ourselves in the anonymous method below
newScreenshot = (sender, e) =>
{
// Chromium has rendered. Tell the task about it.
NewScreenshot -= newScreenshot;
completionSource.TrySetResultAsync(ScreenshotOrNull());
};
NewScreenshot += newScreenshot;
}
else
{
completionSource.TrySetResultAsync(screenshot);
}
return completionSource.Task;
}
示例2: LoadPageAsync
public static Task LoadPageAsync(IWebBrowser browser, string address = null)
{
var tcs = new TaskCompletionSource<bool>();
EventHandler<LoadingStateChangedEventArgs> handler = null;
handler = (sender, args) =>
{
//Wait for while page to finish loading not just the first frame
if (!args.IsLoading)
{
browser.LoadingStateChanged -= handler;
tcs.TrySetResultAsync(true);
}
};
browser.LoadingStateChanged += handler;
if (!string.IsNullOrEmpty(address))
{
browser.Load(address);
}
return tcs.Task;
}