i have a listbox with a bunch of printers and by selecting a printer and pressing the install button, the printer gets installed. the process takes 30-45 seconds where the application freezing but it is installing at the background. is there anyway to place a progress bar that shows something is happening instead of the freezing. this is the part of the code where the printer is installing. doesnt need a progress bar but any type of activity to show something is happening while the driver is installing
Dim objNetwork = CreateObject("WScript.Network")
MsgBox("Printer Driver is Installing, Please wait",, "Installing")
objNetwork.AddWindowsPrinterConnection("\\printserver\" CheckedListBox1.SelectedItem)
objNetwork.SetDefaultPrinter(CheckedListBox1.SelectedItem)
objNetwork = Nothing
thanx in advance
CodePudding user response:
You can really choose any way to tell the user to wait while doing background tasks. Here is an option with a ToolStripStatusLabel and Cursor update. It uses Async/Await to create the Task to not freeze your ui.
Option Strict On
Imports IWshRuntimeLibrary
' ---
Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
Me.Cursor = Cursors.WaitCursor
Me.ToolStripStatusLabel1.Text = "Printer Driver is Installing, Please wait"
Dim name = CheckedListBox1.SelectedItem.ToString()
Await addPrinterAsync(name)
Finally
Me.Cursor = Cursors.Default
Me.ToolStripStatusLabel1.Text = "Ready"
End Try
End Sub
Private Function addPrinterAsync(name As String) As Task
Return Task.Run(
Sub()
Dim objNetwork As New WshNetwork() '= CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection($"\\printserver\{name}")
objNetwork.SetDefaultPrinter(name)
End Sub)
End Function
I have also added a COM reference to Windows Script Host Object Model
which allows you to put Option Strict On
and create the printer with strong types. But that is optional but you can always go without as you have.
I just don't think a MsgBox will work because the user can close it any time they like even if you manage to get it to show non-modal and not freeze the ui.