Running a BackGroundWorker more than once - vb.net

I wonder if you can help?, please bear in mid I’m an absolute novice.
I trying to update a program I made a few years ago for booking in serial numbers into a CRM application.
Currently it runs the following command for each of the 100 textboxes and has worked a treat booking in more than 81000 serial numbers.
If TextBox1.Text.Length > 1 Then
Clipboard.SetText(TextBox1.Text)
RetBat1 = Shell("C:\Windows\BookIN.exe", , True)
Endif
The new version of the app I’ve added a listbox1 with the serial numbers in, I’m then running the below For Each loop.
The For Each loop copies each Item into the clipboard and the BookIN.exe tabs to the right location in the CRM and pastes the information, then clicks a button in the CRM for a new Line and then runs again. This works fine, but I want to add a stop button or a stop checkbox.
For Each items In ListBox1.Items
Clipboard.SetText(items)
RetBat1 = Shell("C:\Windows\BookIN.exe", , True)
Next
I have tried adding the Retbat1 to a backgroundworker, which checks if Checkbox1.checked then exit the for each loop.
The first serial number works, but when the backgroundworker runs more than once I get the following error.
If CheckBox1.Checked = True Then
BackgroundWorker1.CancelAsync()
Else
Dim RetBat1 As String
RetBat1 = Shell("C:\Windows\BookIN.exe", , True)
End If
System.InvalidOperationException: 'This BackgroundWorker is currently busy and cannot run multiple tasks concurrently.'
Sorry if this makes not sense, thanks James

The way it would go is that you would run the BackgroundWorker and have your loop in the DoWork event handler and check whether a cancellation has been requested within that loop. As you've described it, you would then handle the CheckedChanged even of your CheckBox and request the cancellation when the event is raised. I would not use a CheckBox though, because that implies that you can uncheck it to uncancel the work. I would suggest using a Button and handling its Click event, e.g.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'We must get the data from the UI here and pass it into the background thread.
Dim items = ListBox1.Items.Cast(Of String)().ToArray()
'Start the background work and pass in the data.
BackgroundWorker1.RunWorkerAsync(items)
'Enable the Cancel button.
Button2.Enabled = True
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'Disable the Cancel button so it cannot used again.
Button2.Enabled = False
'Request that background processing be cancelled.
BackgroundWorker1.CancelAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
'Get the data that was passed in.
Dim items = DirectCast(e.Argument, String())
'Process each item.
For Each item In items
'Check whether processing has been cancelled.
If BackgroundWorker1.CancellationPending Then
'Cancel processing.
e.Cancel = True
Exit For
End If
'Process the current item here.
Next
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
If e.Cancelled Then
MessageBox.Show("The background operation was cancelled")
Else
'Disable the Cancel button.
Button2.Enabled = False
MessageBox.Show("The background operation completed successfully")
End If
End Sub
The Cancel Button should be disabled by default and notice that this code ensures that it is only enabled when background processing is in progress. Note that the other Button probably ought to be disabled while the processing is in progress too. If you don't do that, at least check IsBusy on the BackgroundWorker. The visual feedback to the user is better though.

Related

How to do a backgroundworker about SAPI SPEAK

I want to create a SAPI speaking background worker in Visual Basic .NET in order to allow my client to continue doing something while listens to the SAPI talk.
I've reached that point, but the problem is if I want to reproduce another speaking, I cannot cancel the current speaking and occurs an exception.
I have the following code:
'MODULE IMPORTED IN THE MAIN WORK: argsBackgroundWorker.vb
Public Class argsBackgroundWorker
Public text_to_speak As String
End Class
Private talk As argsBackgroundWorker = New argsBackgroundWorker()
Private Sub sapitalk_background_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles sapitalk_background.DoWork
If (My.Settings.help_voice = True) Then 'PASS TRUE
Dim reproduceText
Dim text As argsBackgroundWorker = e.Argument
'I have put this code to cancel... So? :(
If sapotalk_background.CancellationPending Then Exit Sub
reproduceText = CreateObject("Sapi.spvoice")
reproduceText.speak(talk.text_to_talk)
Else
sapitalk_background.CancelAsync()
End If
End Sub
Private Sub btn_saysomething_Click(sender As Object, e As EventArgs) Handles btn_saysomething.Click
'Support in order to cancel tasks.
sapitalk_background.WorkerSupportsCancellation = True
talk.text_to_speak = "SOMETHING SOOOOOO SOO SOOOOOO LONG..."
'Cancel another text being spoken.
sapitalk_background.CancelAsync()
'Then, talk the new text.
sapitalk_background.RunWorkerAsync(talk)
End Sub
Private Sub principal_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Support in order to cancel tasks.
sapitalk_background.WorkerSupportsCancellation = True
talk.text_to_speak = "SOMETHING SOOOOOO SOO SOOOOOO LONG..."
'Cancel another text being spoken.
sapitalk_background.CancelAsync()
'Then, talk the new text.
sapitalk_background.RunWorkerAsync(talk)
End Sub
It works great as background speaking but notice that when I compile the app, it starts speaking a long text. And if I click a button to cancel the current speaking and speak another text, it fails and shows me that it is running a current background worker.
The only idea I have is
While Not sapitalk_background.IsBusy
sapitalk_background.RunWorkerAsync(talk)
End While
If it gets stuck in this loop until the voice stops then I am thinking that you cant stop the voice once its running on a bg thread.

Vb.net button still allows click while disabled

My button is responding to clicks while disabled.
Private Sub btnGenerate_Click(sender As Object, e As EventArgs) Handles btnGenerate.Click
btnGenerate.Enabled = False
Me.Cursor = Cursors.WaitCursor
'Do a bunch of operations
Me.Cursor = Cursors.Default
btnGenerate.Enabled = True
End Sub
It takes about 5-10 seconds to process the stuff I'm doing in the background. During that 5-10 seconds the button is greyed out, but if I click it a second time, then it performs the operational stuff a second time after finishing the first.
I'm missing something here. How can I prevent button from allowing interaction until operations are finished?
Dim Working as boolean=false
Private Sub btnGenerate_Click(sender As Object, e As EventArgs) Handles btnGenerate.Click
if Working=true then exit sub
' Your Work Process
Work()
End Sub
sub Work()
Working=True
' Work code
Working=False
end sub
this should prevent the double click
With VS2012, Async Work is very easy to use (compared to previous versions...).
The problem is the UI thread is not letting go. Without seeing what 'work' is actually going on, I can not explain why. I am hoping nothing that re enables the button...
However, Async will allow release of the UI thread and the enabled = false should take effect. Try something like this:
Private Async Sub btnGenerate_Click(sender As Object, e As EventArgs) Handles btnGenerate.Click
btnGenerate.Enabled = False
Dim t As New Task(Sub() MyWorkLoad())
t.Start()
Await t
btnGenerate.Enabled = True
End Sub
Private Sub MyWorkLoad()
'do your work here
'for testing
Dim time As Date = Now
Do While True
If DateAdd(DateInterval.Second, -5, Now) > time Then Exit Do
Loop
End Sub
This did work for me...

Start a process in a VB form without clicking a button

Here is my situation.
I have a windows from application in VB.NET. In the my first form there are two checkboxes. If the one is checked it goes to Form2, if only the second one is checked it goes to Form3. If both are checked then i want first to go on Form2 and when this process is finished go to Form3. All good until this point.
However, I want Form3 to start executing its code (which is inside a background worker) without pressing any button (which is the case when only the second checkbox is checked). Is this possible?
Thanks In advance!
Extra info....
To navigate between the Forms I use a Show/hide/Close scheme. For example:
If CheckBoxTrain.CheckState = CheckState.Checked Then
Me.Hide()
Form2.Show()
ElseIf CheckBoxTrain.CheckState = CheckState.Unchecked And CheckBoxEvaluate.CheckState = CheckState.Checked Then
Me.Hide()
Form3.Show()
At the end of the code in form2 i check if the second checkbox is also checked:
If Form1.CheckBoxTrain.CheckState = CheckState.Checked And Form1.CheckBoxEvaluate.CheckState = CheckState.Checked Then
Form3.Show()
Me.Hide()
End If
Then in Form3 there is a button in which after pressing it i have all the code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For i = 1 To n_monte
Button1.Enabled = False
Button2.Enabled = True
BackgroundWorker1.WorkerSupportsCancellation = True
BackgroundWorker1.WorkerReportsProgress = True
BackgroundWorker1.RunWorkerAsync()
Next
End Sub
All i want is for this last piece of code is to start running without pressing any buttons (only for the case in which both checkboxes are checked)
Declare a Boolean property in Form3 and set that property in Form1 based on whether or not both CheckBoxes are checked. In the Load event handler of Form3, start the BackgroundWorker or don't based on the value of that property.

vb.net multi threading

I have been doing some coding for a system and need to use threading instead of normal timers in VB.NET.
It works fine but the problem lies in the blink timings, when the button is clicked then it blinks as expected, if in testing it is clicked more than once then the blinking time roughly multiplies by the original sleep thread time (750ms), this continues to happen for every click.
What can I do to make the blink not speed up? Below is the code!
Private _flash As Boolean = False
Private Sub btnButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnButton1.Click
_flash = True
Dim FlashThread As New Thread(New ThreadStart(AddressOf FlashLabel))
FlashThread.Start()
End Sub
Private Sub FlashLabel()
Dim _color As Color = Color.Gray
While _flash
If label1.ForeColor.Equals(_color) Then
label1.ForeColor = Color.Red
Else
label1.ForeColor = Color.Gray
System.Threading.Thread.Sleep(750)
End While
End Sub
Every time the button is clicked, you are starting a new thread, so if you click the button twice, it will start two threads, both of which are toggling the colors at 750 millisecond intervals, so it appears as if there was one thread doing it twice as fast. A simple way around this is to simply skip starting the new thread if the _flash flag is already set, for instance:
Private Sub btnButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnButton1.Click
If Not _flash Then
_flash = True
Dim FlashThread As New Thread(New ThreadStart(AddressOf FlashLabel))
FlashThread.Start()
End If
End Sub
You never cancel the original thread, so when you click the button a second time you now have two threads running and causing the blink to happen.
So you can either cancel the thread in another action or only start the thread the first time and then set _flash to true and false after that.

VB.Net: Understanding the way Application.Run() works

Hans Passant gave me a great answer here, so I thought of asking for more details to try to understand the way Application.Run() works.
As far as I understand from the docs, it seems that Application.Run() starts a message loop on the current thread, which in turns enables it to process user input (Is that right?). The overloaded version Application.Run(Form) basically does the same, only it exists when the form closes, and it shows the form by default.
That raises a few questions:
How would one do to simply call from the Main() sub a function that can communicate with the user to (message boxes and so on) and wait for it to exit?
When the message loop is started without a form, how do you launch a new form from this loop, and wait for it to exit? ShowDialog could work, unless you don't want the form to display immediately when launched (eg. if you have a for that's launched minimized to the system tray)
Basically, the situation would be as follows: sub `Main` has a list of tasks to execute in 20mn, with a system tray icon telling the user that the program will operate in 20mn. A timer ticks after 20mns, and has to execute say approx. 15 tasks one by one, every time creating an instance of a progress dialog, initially hidden in the taskbar.
`ShowDialog` would display the form, which is not wanted; so the way I would do it would be to pass the progress dialog a callback to a function that starts the next task. But that wouldn't exit the first progress form before the second has exited, would it? Which means 15 forms would end up being opened...
So the solution may be to invoke (begininvoke?) the callback on the main application loop... Only, I don't know how to do this, because I don't have a form associated with the loop to invoke the callback on...
I hope my questions are clear (I might confuse many things, sorry),
Thanks,
CFP.
Drop a Timer, ProgressBar and a BackgroundWorker on the form. First thing you'll want to do is to prevent the form from getting visible when the program is started. Paste this code into the form class:
Protected Overrides Sub SetVisibleCore(ByVal value As Boolean)
If Not Me.IsHandleCreated Then
value = False
Me.CreateHandle
End If
MyBase.SetVisibleCore(value)
End Sub
Use the timer to get the job started. Set its Interval and Enabled properties, add the Tick event handler:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Me.Show()
ProgressBar1.Visible = True
Me.Enabled = False
BackgroundWorker1.RunWorkerAsync()
End Sub
That makes the form visible when the job is started and starts the background worker. Set the BGW's WorkerReportsProgress property to True and add the 3 event handlers:
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
'' Do stuff here, call BackgroundWorker1.ReportProgress to update the PB
End Sub
Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
ProgressBar1.Value = e.ProgressPercentage
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
ProgressBar1.Visible = False
Me.Enabled = True
Me.Hide()
End Sub
It is up to you to fill in the code for the DoWork event handler. Have it do those 15 jobs, be sure to call BackgroundWorker1.ReportProgess so that the progress bar gets updated. Which is what the ProgressChanged event handler does. The RunWorkerCompleted event handler hides the form again.
You can call the Show() method in the context menu item event for the NotifyIcon so that the user can make your form visible again. Call Application.Exit() in the context menu item that allow the user to quit your app. Make sure you disable that when the BGW is running. Or implement a way to cleanly stop the job.