Cancelling a TaskCompletionSource wait

This page summarizes the projects mentioned and recommended in the original post on /r/CefSharp

Our great sponsors
  • WorkOS - The modern identity platform for B2B SaaS
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • SaaSHub - Software Alternatives and Reviews
  • CefSharp

    .NET (WPF and Windows Forms) bindings for the Chromium Embedded Framework

  • Imports System.Threading Imports CefSharp Imports CefSharp.WinForms Public Class Waits Private Cancellation_Token_Source As CancellationTokenSource Private Property Chromium As ChromiumWebBrowser Sub New(Chromium As ChromiumWebBrowser) Me.Chromium = Chromium End Sub Public Sub Create_Cancellation_Token() ' Needs to be created the first time and after a cancel. Otherwise, we can just use the same one. If Cancellation_Token_Source Is Nothing OrElse Cancellation_Token_Source.IsCancellationRequested Then Cancellation_Token_Source = New CancellationTokenSource End Sub Public Sub Cancel() Cancellation_Token_Source.Cancel() Cancellation_Token_Source.Dispose() End Sub Public Function Page_Load_Task() As Task ' Original source: https://github.com/cefsharp/CefSharp/blob/cefsharp/83/CefSharp.OffScreen.Example/Program.cs#L127 Dim Handler As EventHandler(Of LoadingStateChangedEventArgs) Dim Task = New TaskCompletionSource(Of Boolean)(TaskCreationOptions.RunContinuationsAsynchronously) Dim Cancellation_Token As CancellationToken = Cancellation_Token_Source.Token Cancellation_Token.Register(Sub() Task.TrySetCanceled()) Handler = Sub(Sender, Arguments) ' Wait for the entire page to finish loading and not just the first frame. If Not Arguments.IsLoading Then RemoveHandler Chromium.LoadingStateChanged, Handler ' It is important that the continuation runs async via TaskCreationOptions.RunContinuationsAsynchronously. Task.TrySetResult(True) End If End Sub AddHandler Chromium.LoadingStateChanged, Handler Return Task.Task End Function Public Async Function Page_Load() As Task ' Wrapper to silently catch exception upon cancellation. Try Await Page_Load_Task() Catch TaskCanceledException As Exception End Try End Function Public Function Url_Change_Task(Fragment As String) As Task ' Waits for page to load with a url that end with the fragment. (Modifed from Page_Load().) Dim Handler As EventHandler(Of AddressChangedEventArgs) Dim Task = New TaskCompletionSource(Of Boolean)(TaskCreationOptions.RunContinuationsAsynchronously) Dim Cancellation_Token As CancellationToken = Cancellation_Token_Source.Token Cancellation_Token.Register(Sub() Task.TrySetCanceled()) Handler = Sub(Sender, Arguments) If Arguments.Address.EndsWith(Fragment) Then RemoveHandler Chromium.AddressChanged, Handler Task.TrySetResult(True) End If End Sub AddHandler Chromium.AddressChanged, Handler Return Task.Task End Function Public Async Function Url_Change(Fragment As String) As Task ' Wrapper to silently catch exception upon cancellation. Try Await Url_Change_Task(Fragment) Catch TaskCanceledException As Exception End Try End Function Public Sub Sleep(Optional Deciseconds As Integer = 1) For Second As Integer = 1 To Deciseconds If Cancellation_Token_Source.IsCancellationRequested Then Exit For Thread.Sleep(100) Application.DoEvents() Next End Sub End Class

  • WorkOS

    The modern identity platform for B2B SaaS. The APIs are flexible and easy-to-use, supporting authentication, user identity, and complex enterprise features like SSO and SCIM provisioning.

    WorkOS logo
NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts