-
Mar 26th, 2021, 12:29 PM
#1
Thread Starter
Member
[RESOLVED] Webview2 Download Not Firing
ADMIN: Please move to VB.NET Sub Forum. Apologies. Many thanks!
Using WebView2 (webVwLAF), I am trying to manually download and open a PDF from a postback link from a site. Unfortunately, when I use "wait(10) to allow the download to finish and to be able to open the file thereafter, the "click" in ExecuteScriptAsync doesn't fire and download doesn't start. But, if I remove "wait(10)", click ExecuteScriptAsync fires up the download.
Any help on this? Or any recommendations/sample how to catch download finished event of webview2?
Thanks!
Code:
Private Sub webVwLAF_NavigationCompleted(sender As Object, e As CoreWebView2NavigationCompletedEventArgs) Handles webVwLAF.NavigationCompleted
webVwLAF.Select()
If FirstRunLAF Then
webVwLAF.ExecuteScriptAsync("document.getElementById('lnklaf').click();")
Try
Wait(10)
WebAddress = "C:/Users/pga/Downloads/Laf.pdf"
webVwLAF.Source = New Uri(WebAddress)
webVwLAF.CoreWebView2.Navigate(WebAddress)
Catch ex As UriFormatException
MessageBox.Show("Full URL ex -> HTTP[S]")
Catch ex As Exception
MessageBox.Show(ex.Message, "Error...")
End Try
FirstRunLAF = False
End If
End Sub
Public Sub Wait(ByVal seconds As Double)
Static start As Date
start = Now()
Do While Now() < start.AddSeconds(seconds)
System.Windows.Forms.Application.DoEvents()
Loop
End Sub
Last edited by pidyok; Mar 26th, 2021 at 12:36 PM.
-
Mar 26th, 2021, 01:01 PM
#2
Re: Webview2 Download Not Firing
Originally Posted by pidyok
ADMIN: Please move to VB.NET Sub Forum. Apologies. Many thanks!
Done
-
Mar 26th, 2021, 09:10 PM
#3
Re: Webview2 Download Not Firing
Do not EVER use a busy-wait like that. A busy-wait is when you are constantly doing work to achieve nothing. That loop will use 100% of the CPU in order to do nothing. That's bad for all sorts of reasons.
You could use a Timer with an Interval of 10 seconds and then do the remaining work on the Tick event. These days, you could also use Task.Delay and not even have to split the code. You could use it as a drop-in replacement for your horrible Wait method, although you will have to use Async/Await.
vb.net Code:
Private Async Sub webVwLAF_NavigationCompleted(sender As Object, e As CoreWebView2NavigationCompletedEventArgs) Handles webVwLAF.NavigationCompleted
'...
Await Task.Delay(TimeSpan.FromSeconds(10))
'...
End If
-
Mar 27th, 2021, 05:26 AM
#4
Thread Starter
Member
Re: Webview2 Download Not Firing
-
Mar 27th, 2021, 05:43 AM
#5
Re: Webview2 Download Not Firing
Originally Posted by pidyok
Admin, pls mark this thread solved.
Use the Thread Tools menu to mark the thread Resolved for yourself.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|