Cefsharp download window for showing progress in VB.Net - vb.net

I am creating a browser using Cefsharp in VB.Net, and I have been trying to create a download window that shows the progress of the current download. I don't know if I am doing something wrong or if it is the way CEF works, but I put in a download handler by adding browser.DownloadHandler = New DownloadHandler to Form1_Load and creating a new class like this (with downloading being the form I created for showing the progress):
Public Class DownloadHandler
Implements IDownloadHandler
Public Function OnBeforeDownload(downloadItem As DownloadItem, ByRef downloadPath As String, ByRef showDialog As Boolean) As Boolean Implements IDownloadHandler.OnBeforeDownload
downloadPath = downloadItem.SuggestedFileName
showDialog = True
downloading.Show()
Return True
End Function
Public Function OnDownloadUpdated1(downloadItem As DownloadItem) As Boolean Implements IDownloadHandler.OnDownloadUpdated
My.Settings.downloadpercent = downloadItem.PercentComplete.ToString
Return False
End Function
End Class
On the downloading form I have this code for showing the progress:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim percentcomplete As Integer = My.Settings.downloadpercent * 5
Me.PictureBox1.Size = New Size(percentcomplete, 25)
End Sub
Private Sub downloading_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer1.Start()
End Sub
This may not be the best way to show a ProgressBar, but I have a picture box that just has a green bar, and the total width of the form is 500px. The code is telling it to read the PercentComplete setting put in My.Settings.downloadpercent, multiply it by 5, so when the progress in 100%, it will go across the whole form.
The problem is that the ProgressBar is not being updated to show the current progress. It goes a little bit, but then it just stops. Am I doing something wrong, or is OnDownloadUpdated not a good place to put that? Any suggestions of how to fix this?
Edit:
I am using CefSharp 39.0.0-pre03. Also, when the save file dialog comes up, no matter if you click Save or Cancel, the browser always triggers a LoadError, so it loads the custom HTML error page I made, and since it requires a URL for loading HTML, I put in "http://rendering/"... So I guess that would be a domain change. That issue (in the comments) could be the problem, but then we also need to figure out why it is triggering a LoadError.

A simple solution that may be of interest to those who faced the same or similar problem:
Declare Public your Picturebox, in a module:
Module Module1
Public PictureBox1 As New PictureBox
End Module
In MainForm you can declare the attributes you want (after the InitializeComponent). Example:
With PictureBox1
.BorderStyle = Border3DStyle.Flat
.Image = Bitmap.FromFile(YourImageFileName)
'......
End With
In the DownloadHandler Class:
Public Function OnDownloadUpdated1(downloadItem As DownloadItem) As Boolean
Implements IDownloadHandler.OnDownloadUpdated
Dim percentcomplete As Integer=downloadItem.PercentComplete * 5
PictureBox1.Size = New Size(percentcomplete, 25)
Return False
End Function

Related

CrystalReportViewer.RefreshReport hangs when running from BackgroundWorker

I'm trying to "enhance" my reporting code by adding a loading screen while the Crystal Report is being prepared/loaded. Before I started trying to add the loading screen, all of my reports would come up just fine, but the cursor change just wasn't "enough" of an indication that the application was still working on pulling the report - some of them can take a while - so I wanted to provide a more "obvious" visual cue.
In order to accomplish this, I've put the report creation method calls into a BackgroundWorker that exists in the loading screen itself (I haven't gotten around to learning how to use Async/Await well enough yet to feel comfortable using that instead). The loading screen comes up correctly and everything appears to work as expected until it actually attempts to display the report on screen. At that point, the "Please wait while the document is processing." box comes up (in the CrystalReportViewer control in the form used to display reports), but it just sits there, not even spinning. Eventually, my IDE throws an error about receiving a ContextSwitchDeadlock and I pretty much just have to cancel execution.
Here's my dlgReportLoading "splash screen" with a PictureBox control that contains an animated GIF:
Imports System.Windows.Forms
Public Class dlgReportLoading
Private DisplayReport As Common.CRReport
Private WithEvents LoadReportWorker As System.ComponentModel.BackgroundWorker
Public Sub New(ByRef Report As Common.CRReport)
InitializeComponent()
DisplayReport = Report
End Sub
Private Sub dlgReportLoading_Load(sender As Object, e As EventArgs) Handles Me.Load
Me.Cursor = Cursors.WaitCursor
Me.TopMost = True
Me.TopMost = False
LoadReportWorker = New System.ComponentModel.BackgroundWorker
LoadReportWorker.RunWorkerAsync()
End Sub
Private Sub dlgReportLoading_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
Me.Cursor = Cursors.Default
End Sub
Private Sub LoadReport_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles LoadReportWorker.DoWork
If Not DisplayReport.ReportOption = Common.CRReport.GenerateReportOption.None Then
Select Case DisplayReport.ReportOption
Case Common.CRReport.GenerateReportOption.DisplayOnScreen
'-- This is the method I'm currently testing
DisplayReport.ShowReport()
Case Common.CRReport.GenerateReportOption.SendToPrinter
DisplayReport.PrintReport()
Case Common.CRReport.GenerateReportOption.ExportToFile
DisplayReport.ExportReport()
End Select
End If
DisplayReport.ReportOption = Common.CRReport.GenerateReportOption.None
'--
'-- This code was in use before trying to generate the reports in the background
'If Not DisplayReport.CrystalReport Is Nothing Then
' DisplayReport.CrystalReport.Dispose()
'End If
'--
End Sub
Private Sub LoadReport_Complete(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles LoadReportWorker.RunWorkerCompleted
Me.DialogResult = DialogResult.OK
Me.Close()
End Sub
End Class
As noted in the code above, I'm currently testing the ShowReport() method as defined here:
Protected Friend Sub ShowReport()
Dim ReportViewer As frmReportPreview
Me.PrepareReport()
ReportViewer = New frmReportPreview(Me)
With ReportViewer
.WindowState = FormWindowState.Maximized
.Show()
End With
End Sub
And the frmReportPreview is this:
Imports System.ComponentModel
Public Class frmReportPreview
Private DisplayReport As Common.CRReport
Private ReportToDisplay As CrystalDecisions.CrystalReports.Engine.ReportDocument
Public Sub New(ByRef Report As Common.CRReport)
InitializeComponent()
DisplayReport = Report
PrepareReportForDisplay()
Me.rptViewer.ReportSource = Nothing
Me.rptViewer.ReportSource = ReportToDisplay
' SET ZOOM LEVEL FOR DISPLAY:
' 1 = Page Width
' 2 = Whole Page
' 25-100 = zoom %
Me.rptViewer.Zoom(1)
Me.rptViewer.Show()
End Sub
Private Sub frmReportPreview_Shown(sender As Object, e As EventArgs) Handles Me.Shown
'-- HANGS HERE
Me.rptViewer.RefreshReport()
End Sub
Private Sub frmReportPreview_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
ReportToDisplay.Dispose()
Me.rptViewer.ReportSource = Nothing
End Sub
'...CODE FOR PREPARING THE REPORT TO BE DISPLAYED
End Class
The dlgReportLoading form pops up correctly and the animation plays until the frmReportPreview pops up in front of it (it doesn't close). The little box that has what is normally an animated spinning circle indicating the report data is being loaded appears, but almost immediately freezes in place.
I have a breakpoint in the LoadReport_DoWork() method of my dlgReportLoading form after the call to the ShowReport() method, but it never gets to that point. I also have one in the LoadReport_Complete() method of that form that it never hits either and that dialog never actually closes.
I put another breakpoint at the end of the frmReportPreview_Shown method, right after the Me.rptViewer.RefreshReport() call, but it never hits that either, so it seems clear that this is where things are getting stuck, but only when the report is being generated through the BackgroundWorker. If I just call the ShowReport() method without sending it through the "splash screen" and BackgroundWorker, everything generates and displays normally.
I've tried putting the RefreshReport() method into its own BackgroundWorker with no change in the behavior. I've tried making the frmReportPreview object display modally with ShowDialog() instead of just Show(). None of this seems to help the issue.
I have a feeling something is being disposed of too early somewhere, but I can't figure out what that would be. I can provide the rest of the report preparation code from frmReportPreview if required, but that all seems to be working without error, as far as I can tell. I'm not averse to trying alternate methods of accomplishing my goal of showing the user a loading screen while all the report preparation is taking place - e.g., Async/Await or other multi-threading methods - so any suggestions are welcome. Please let me know if any additional clarification is needed.
ENVIRONMENT
Microsoft Windows 10 Pro 21H1 (OS build 19043.1348)
Microsoft Visual Studio Community 2017 (v15.9.38)
Crystal Reports for .NET Framework v13.0.3500.0 (Runtime version 2.0.50727)
EDIT: I forgot to mention that this whole mess is being called from a GenerateReport() method in my CRReport class defined as:
Public Sub GenerateReport(ByVal ReportGeneration As GenerateReportOption)
Me.ReportOption = ReportGeneration
If Me.ReportOption = GenerateReportOption.None Then
'...CODE FOR REQUESTING A GENERATION OPTION FROM THE USER
End If
Dim ReportLoadingScreen As New dlgReportLoading(Me)
ReportLoadingScreen.ShowDialog()
End Sub
Which, in turn, is being called from my main form like this:
Private Sub PrintMyXMLReport(ByVal XMLFile As IO.FileInfo)
Dim MyXMLReport As New IO.FileInfo("\\SERVER\Applications\Reports\MyXMLReport.rpt")
Dim Report As New Common.CRReport(MyXMLReport, XMLFile)
Report.GenerateReport(Common.CRReport.GenerateReportOption.DisplayOnScreen)
End Sub
You should separate the heavy lifting and UI operations into distinct methods in order to put them into the appropriate BackgroundWorker events:
Protected Friend Sub PrepareReport()
' perform long-running background work
End Sub
Protected Friend Sub ShowReport()
Dim ReportViewer = New frmReportPreview(Me) With {.WindowState = FormWindowState.Maximized}
ReportViewer.Show()
End Sub
Private DisplayReport As Common.CRReport
Private Sub LoadReport_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles LoadReportWorker.DoWork
DisplayReport.PrepareReport()
End Sub
Private Sub LoadReport_Complete(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles LoadReportWorker.RunWorkerCompleted
DisplayReport.ShowReport()
Me.DialogResult = DialogResult.OK
Me.Close()
End Sub
because LoadReport_DoWork actually runs on a new non-UI thread, and LoadReport_Complete runs on the caller thread, which is a UI thread. Only there can you interact with the UI and show Forms etc.

How to handle long running tasks in VB.NET forms?

I am currently working on a VB.NET form that automatically create Word documents according to an Excel file and a few extra data asked by the form (Project Name, Customer Name, Use SQL, ...).
This procedure works fine and takes approximatelly 1 or 2 minutes to complete.
The issue is that all my script is in ButtonGenerate.Click Handler. So when the Generate button is pressed the form window is bricked and it's impossible to Cancel...
It shouldn't be in a Click handler. Opening a new thread for that long task seems better. But Multithreading isn't very familiar to me.
I tryed launching the script with
ThreadPool.QueueUserWorkItem(...
but my Generate Sub sets labels and update a Progress Bar in the main form, so I doesn't work unless I use
Me.Invoke(New MethodInvoker(Sub()
label.Text = "..."
ProgressBar.Value = 10
' ...
End Sub)
each time I need to update something on the form and I can't even retrieve any new push of a button with that (A cancel button would be nice).
This is basically my code :
Public Class TestFichesAutomation
Private Sub BtnGenerate_Click(sender As Object, e As EventArgs) Handles BtnGenerate.Click
System.Threading.ThreadPool.QueueUserWorkItem(Sub() Generate())
End Sub
Public Sub Generate()
' Check user options, retrieve Excel Data, SQL, Fill in custom classes, create Word docs (~ 1 minute)
End Sub
So How would you handle that script ? Is Threading even a good solution ?
Thanks a lot for your help ^^ and for the useful doc.
My app now open a new thread and uses 2 custom classes to act like buffers :
Private Async Sub Btn_Click(sender As Object, e As EventArgs) Handles Btn.Click
myProgress = New Progress
' a custom class just for the UI with the current task, current SQL connection status and progress value in %
_Options.ProjectName = TextBoxProjectName.Text
_Options.CustomerName = TextBoxCustomerName.Text
...
' Fill in a custom "_Options" private class to act as a buffer between the 2 thread (the user choices)
Loading = New Loading()
Me.Visible = False
Loading.Show() ' Show the Loading window (a ProgressBar and a label : inputLine)
Task.Run(Function() Generate(Progress, _Options))
Me.Visible = True
End Sub
Public Async Function Generate(ByVal myProgress As Progress, ByVal Options As Options) As Task(Of Boolean)
' DO THE LONG JOB and sometimes update the UI :
myProgress.LoadingValue = 50 ' %
myProgress.CurrentTask= "SQL query : " & ...
Me.Invoke(New MethodInvoker(Sub() UpdateLoading()))
' Check if the task has been cancelled ("Cancelled" is changed by a passvalue from the Loading window):
If myProgress.Cancelled = True Then ...
' Continue ...
End Function
Public Shared Sub UpdateLoading()
MyForm.Loading.ProgressBar.Value = myProgress.LoadingValue
MyForm.Loading.inputLine.Text = myProgress.CurrentTask
' ...
End Sub
You should look into using the Async/Await structure
if the work you need to do is CPU bound, i like using Task.Run() doc here
By making your event handler Async and having it Await the work, you prevent the UI from locking up and avoid the use of Invoke in most cases.
ex:
Private Async Sub Btn_Click(sender As Object, e As EventArgs) Handles Btn.Click
Dim Result As Object = Await Task.Run(Function() SomeFunction())
'after the task returned by Task.Run is completed, the sub will continue, thus allowing you to update the UI etc..
End Sub
For progress reporting with Async/Await you might be interested in this

How to store a variable after form closes for use later on another form

So I am building a form based application, and I am running into an issue with passing data between forms. I have a combo box that, based on selection, triggers a new form to open with several buttons to pick from. Once you select a button, the form closes, but I can't get the selection to be carried over to the original form.
basic idea of the code is like this
Public Class frmMain
Public intStore As integer
Private Sub cboSample_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboSample.SelectedIndexChanged
Dim selection as Integer
selection = cboSample.SelectedIndex
If selection = -1 Then
Else
Select Case selection
Case 0
frmOne.Show()
Case 1
frmTwo.Show()
End Select
End If
End Sub
End Class
Here is a sample of the second form code
Public Class frmOne
Public storage As varStorage
Private Sub btn_Clicked(sender As Object, e As EventArgs) Handles btn.Clicked
storage = New varStorage With {.datastore = 1}
Me.Close()
End Sub
End Class
frmTwo is pretty much the same but handles more options
The Class I created looks like this
Public Class varStorage
Public _dataStore As Integer
Public Property dataStore() As Integer
Get
Return _dataStore
End Get
Set (value As Integer)
_dataStore = value
End Set
End Property
End Class
as I said, the issue comes from the point of the form being called, and the form closing, the variable data is not being saved. I am almost certain I am missing some code somewhere, but not sure where. any help would be greatly appreciated.
Rather than using Show() to open another form, using ShowDialog() will open the form modally. The form will behave like a MessageBox. It will wait for you to respond to, and close, this form before returning to your main form. We can obtain a value from this modal-form, safe in the knowledge that the form has been closed (and so the value we have obtained will not be changed).
The other way is to make a Public Property of the form. then you can create Get and Set methods to have access to your form objects
Public Class yourFormClass
Public Property Note As String
Get
Return txtNote.Text
End Get
Set(value As String)
End Set
End Property
End Class
and Then you can use it like
dialog = New yourFormClass()
someOtherTextbox.Text = dialog.Note

Using animated gif in splash screen vb.net

I'm having trouble creating a .gif be animated in my already created visual studio 2013 project.
Been looking online a bit for the "proper" way to do it. Following this MSDN question: Link I copied some of the code to see how it would work. I've also seen references to using a timer.
Public Not Inheritable Class SplashScreen
Private progressGifPath As String = "C:\Documents\Visual Studio 2013\Projects\CameraFinder\icons" + "\orangeLoading.gif"
Private _AnimatedGif As New Bitmap(progressGifPath)
Private m_IsAnimating As Boolean
Public Property IsAnimating() As Boolean
Get
Return m_IsAnimating
End Get
Set(ByVal value As Boolean)
m_IsAnimating = value
End Set
End Property
Private Sub VICameraFinderLoading_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Set up the dialog text at runtime according to the application's assembly information.
If My.Application.Info.Title <> "" Then
Else
End If
End Sub
Private Sub PlayGIF()
If Not m_IsAnimating Then
ImageAnimator.Animate(_AnimatedGif, New EventHandler(AddressOf Me.OnFrameChanged))
m_IsAnimating = True
End If
End Sub
Private Sub OnFrameChanged(ByVal o As Object, ByVal e As EventArgs)
If m_IsAnimating Then
ImageAnimator.UpdateFrames()
Dim aGraphics As Graphics = PictureBox.CreateGraphics
aGraphics.DrawImage(_AnimatedGif, New Point(30, 30))
aGraphics.Dispose()
End If
End Sub
End Class
So I tried this, but I don't see anything on the picture box drawn (I confirmed to see it was brought to the front). And apparently there can be a size limit to the gif included. Any ideas on what would be the better way to include it?
Thanks!
Figured it out. I hadn't set the image of the picture box to the gif.
EDIT:
To clarify, the code above wasn't the problem at all. I actually didn't need it in any way. I removed it from the splash screen class, completely. The solution was to go into my picture box's property (F4) and set the backgroundImage to the .gif's filepath (thus importing it as an embedded resource). Doing so has allowed for animation in the splash screen.

Populating a DataGridView on-the-fly (VB.NET)

I have a DataGridView which reads data from a custom (CSLA) list object. The dataset might have 100,000 records, and obviously I don't want to show them all at once because, for one, it would take ages to load.
I think it would be nice if I could read the first (for instance) 20 records, and make it so upon scrolling to the end of the list, it reads the next 20 and adds them to the DataGridView.
I have experimented with various ways of doing this, mostly using a custom class inheriting from DataGridView to capture scroll events and raising an event which adds new records. I will include the code below:
Public Class TestDGV
Inherits DataGridView
Public Sub New()
AddHandler Me.VerticalScrollBar.ValueChanged, AddressOf vsScrollEvent
AddHandler Me.RowsAdded, AddressOf vsRowsAddedEvent
End Sub
Private Sub vsScrollEvent(ByVal sender As Object, _
ByVal e As EventArgs)
With DirectCast(sender, ScrollBar)
If .Value >= (.Maximum - .LargeChange) Then
RaiseEvent ScrollToEnd()
End If
End With
End Sub
Private Sub vsRowsAddedEvent(ByVal sender As Object, ByVal e As EventArgs)
ScrollbarOn()
End Sub
Public Event ScrollToEnd()
Public Sub ScrollbarOff()
Me.VerticalScrollBar.Enabled = False
End Sub
Public Sub ScrollbarOn()
Me.VerticalScrollBar.Enabled = True
End Sub
End Class
Though this (sort of) works, it can be buggy. The biggest problem was that if you used the mouse to scroll the DataGrid, it would get stuck in a loop as it process the scrollbar's ValueChanged event after the data was added. That's why I added ScrollbarOff and ScrollbarOn - I call them before and after getting new records, which disables the scrollbar temporarily.
The problem with that is that after the scrollbar is re-enabled, it doesn't keep track of the current mouse state, so if you hold down the 'Down' button with the mouse (or click on part of the scrollbar) it stops scrolling after it has added the new records, and you have to click it again.
Also, it doesn't seem a particularly elegant way of doing things.
Has anyone ever done this before, and how did you achieve it?
Cheers.
Perhaps you could reduce your result set and use the << and >> kind of paging controls. Nerd Dinner demo introduced an elegant solution using LINQ:
//
// GET: /Dinners/
// /Dinners/Page/2
public ActionResult Index(int? page)
{
const int pageSize = 20;
var upcomingDinners = dinnerRepository.FindUpcomingDinners();
var paginatedDinners = upcomingDinners.Skip((page ?? 0) * pageSize)
.Take(pageSize)
.ToList();
return View(paginatedDinners);
}
What about using the virtualproperty of the DataGridView?
See Implementing Virtual Mode with Just-In-Time Data Loading in the Windows Forms DataGridView Control for an example.