Our great sponsors
-
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.
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
Related posts
- Head-up-Display Stream Deck Plugin
- Why use AppDomains when we have processes?
- Chrome extensions in .NET web view controls
- Anyone know why when i go to task manager and look at lively wallpaper this virus is hiding there cefsharp.browsersubprocess?
- [Connectwise] Hey Connectwise, why is duo telling me the Chromium Version in Automate is over a year out of date?