PeterBaileyUk
asked on
Background worker passing messagebox yes no
I have a background worker but its doing a delete operation so it has to have some interaction with a yes no cancel dialogue but I dont know how to integrate that into the background worker to avoid the cross thread error. Ive added the code in sections hopefully to make it easier, apologies if not.
sub that gets called with the messagebox
Private Sub startAsyncButton8_Click(ByVal sender As System.Object,
ByVal e8 As System.EventArgs) Handles DeleteAllTagWordsToolStripMenuItem.Click
If BackgroundWorker8.IsBusy <> True Then
ResultSaveTag.Visible = True
LblResultDeletetags.Text = "Deleting All Tag Words"
Me.ProgressBar9.Visible = True
Me.ProgressBar9.Enabled = True
BackgroundWorker8.RunWorkerAsync()
End If
End Sub
Private Sub backgroundWorker8_DoWork(ByVal sender As System.Object,
ByVal e8 As DoWorkEventArgs) Handles BackgroundWorker8.DoWork
Dim worker8 As BackgroundWorker = CType(sender, BackgroundWorker)
Call DeleteAllTags()
End Sub
Private Sub backgroundWorker8_ProgressChanged(ByVal sender As System.Object,
ByVal e8 As ProgressChangedEventArgs) Handles BackgroundWorker8.ProgressChanged
'ResultLabel.Text = "Started Processing" + (e.ProgressPercentage.ToString() + "%")
Me.ProgressBar9.Enabled = True
Me.ProgressBar9.Visible = True
End Sub
Private Sub backgroundWorker8_RunWorkerCompleted(ByVal sender As Object, ByVal e8 As RunWorkerCompletedEventArgs) Handles BackgroundWorker8.RunWorkerCompleted
If e8.Cancelled = True Then
'LblResultDeletetags.Text = "Canceled!"
Me.ProgressBar9.Enabled = False
Me.ProgressBar9.Visible = False
LblResultDeletetags.Visible = False
ElseIf e8.Error IsNot Nothing Then
LblResultDeletetags.Text = "Error: " & e8.Error.Message
Me.ProgressBar9.Enabled = False
Me.ProgressBar9.Visible = False
LblResultDeletetags.Visible = False
Else
Me.ProgressBar9.Enabled = False
Me.ProgressBar9.Visible = False
LblResultDeletetags.Visible = False
End If
End Sub
BackgroundWorker8.WorkerReportsProgress = True
BackgroundWorker8.WorkerSupportsCancellation = True
sub that gets called with the messagebox
Private Sub DeleteAllTags()
Dim StrProcName As String
Dim connectionString As String = ConfigurationManager.ConnectionStrings("Dictionary").ConnectionString
Select Case MsgBox("Do you really want to delete all saved tagwords?", MsgBoxStyle.YesNoCancel, "Delete Tagwords Dialogue")
Case MsgBoxResult.Yes
MessageBox.Show("Yes button")
StrProcName = "usp_DeleteALLTagWords"
Using conn As New SqlConnection(connectionString)
Using cmd As New SqlCommand(StrProcName, conn)
cmd.CommandTimeout = 0
cmd.CommandType = CommandType.StoredProcedure
conn.Open()
cmd.ExecuteNonQuery()
conn.Close()
End Using
End Using
Case MsgBoxResult.Cancel
'MessageBox.Show("Cancelled")
BackgroundWorker8.CancelAsync()
Me.ProgressBar9.Enabled = False
Me.ProgressBar9.Visible = False
LblResultDeletetags.Visible = False
Case MsgBoxResult.No
'MessageBox.Show("NO Chosen")
BackgroundWorker8.CancelAsync()
Me.ProgressBar9.Enabled = False
Me.ProgressBar9.Visible = False
LblResultDeletetags.Visible = False
End Select
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I assume this way the async handles nothing
Private Sub startAsyncButton8_Click(ByVal sender As System.Object,
ByVal e8 As System.EventArgs) Handles DeleteAllTagWordsToolStripMenuItem.Click
ASKER
so it becomes:
Private Sub startAsyncButton8_Click(ByVal sender As System.Object,
ByVal e8 As System.EventArgs)
If BackgroundWorker8.IsBusy <> True Then
ResultSaveTag.Visible = True
LblResultDeletetags.Text = "Deleting All Tag Words"
Me.ProgressBar9.Visible = True
Me.ProgressBar9.Enabled = True
BackgroundWorker8.RunWorkerAsync()
End If
End Sub
Agreed...ask that question in your "startAsyncButton8" button handler and only start the backgroundworker if necessary. It'd be a different story if you needed multiple responses during the process, but that doesn't appear to be the case.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ok I think this is it:
Private Sub DeleteAllTagWordsToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles DeleteAllTagWordsToolStripMenuItem.Click
Select Case MsgBox("Do you really want to delete all saved tagwords?", MsgBoxStyle.YesNoCancel, "Delete Tagwords Dialogue")
Case MsgBoxResult.Yes
MessageBox.Show("Yes button")
Call startAsyncButton8()
Case MsgBoxResult.Cancel
Case MsgBoxResult.No
End Select
End Sub
Private Sub startAsyncButton8()
If BackgroundWorker8.IsBusy <> True Then
ResultSaveTag.Visible = True
LblResultDeletetags.Text = "Deleting All Tag Words"
Me.ProgressBar9.Visible = True
Me.ProgressBar9.Enabled = True
BackgroundWorker8.RunWorkerAsync()
End If
End Sub
That should work ... and as Mike said, you really should be using MessageBox instead of MsgBox. I'd personally do it like this:
If Messagebox.Show("Are you sure you want to delete all Tag Words?", "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
'/ do the work here
End If
This would fire you code only if the user clicks Yes.
ASKER
is this the bad piece of messagebox code:
Select Case MsgBox("Do you really want to delete all saved tagwords?", MsgBoxStyle.YesNoCancel, "Delete Tagwords Dialogue")
ASKER
ok our messages crossed ok I will correct that now it all appeared to work ok
ASKER
are you happy for me to share the points?
I'd just give all the points to Scott.
ASKER
thank you both
ASKER