Link to home
Start Free TrialLog in
Avatar of PeterBaileyUk
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.



    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

Open in new window

    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

Open in new window


    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

Open in new window



    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

Open in new window


        BackgroundWorker8.WorkerReportsProgress = True
        BackgroundWorker8.WorkerSupportsCancellation = True

Open in new window


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

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Scott McDaniel (EE MVE )
Scott McDaniel (EE MVE )
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of PeterBaileyUk
PeterBaileyUk

ASKER

I didnt think that was possible ok I will try calling the async manually and doing the messagebox first, its a great idea
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

Open in new window

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

Open in new window

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
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

Open in new window

    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

Open in new window

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

Open in new window

This would fire you code only if the user clicks Yes.
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")

Open in new window

ok our messages crossed ok I will correct that now it all appeared to work ok
are you happy for me to share the points?
I'd just give all the points to Scott.
thank you both