Threading question

I really need some guidance with this threading issue I have.

All I want to do is signify to my main program that a thread has been completed or cancelled by the user. I have the following code:

.
.
.

        thdPrintPreview = New Thread(AddressOf PrintPreviewResults)
        thdPrintPreview.Priority = ThreadPriority.AboveNormal
        thdPrintPreview.Name = "Print Preview"
        thdPrintPreview.IsBackground = True
        thdPrintPreview.Start()

    End Sub

    Private Sub PrintPreviewResults()

        Try
            DataGrid2.PrintPreview(CType(obj, DataView), CType(obj2, DataTable), CType(Me.BindingContext(DataGrid2.DataSource), CurrencyManager), 0, "No", frmSignon.dsFNUsers.FNUser.Rows(0).Item("EMAIL"), strFileName)
            thdPrintPreview.Abort()
        Catch exException As ThreadAbortException
            If (thdPrintPreview.ThreadState = ThreadState.AbortRequested) Or (thdPrintPreview.ThreadState = ThreadState.Running) Then
                thdPrintPreview.Join(4000)
                If (thdPrintPreview.ThreadState <> ThreadState.Stopped) Or (thdPrintPreview.ThreadState <> ThreadState.Aborted) Then
                    sbStatusBar.Text = "Cannot terminate thread " & thdPrintPreview.Name
                Else
                    MessageBox.Show("Your PrintPreview has completed.", "PrintPreviewResults", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly)
                    EnableControls()
                End If
            Else
                MessageBox.Show("Your PrintPreview has completed.", "PrintPreviewResults", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly)
                EnableControls()
            End If
        Finally
            If (thdPrintPreview.ThreadState <> ThreadState.Stopped) Or (thdPrintPreview.ThreadState <> ThreadState.Aborted) Then
                sbStatusBar.Text = "Cannot terminate thread " & thdPrintPreview.Name
            Else
                MessageBox.Show("Your PrintPreview has completed.", "PrintPreviewResults", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly)
                EnableControls()
            End If
        End Try

        sbStatusBar.Text = "Ready..."

    End Sub


I'm expecting that after the thread is completed, that it will hit the "abort" method in the "Try" block. However, after the thread completes, no code is executed. I have breakpoints inside the "try" and "catch" blocks.

Interestingly enough, after the thread starts while debugging, I'm expecting the code to go into the PrintPreviewResults procedure and execute the first statement inside the "Try" block. The thread does execute, but I can't get to that point inside the "Try" block.

I also want to find out if someone cancels the thread, to test to see if it's still active. The purpose of testing to see if the thread was completed or cancelled, is to enable some controls on the form. I don't want to enable any controls on the form if the thread is still active.

I've read some stuff inside books and on the internet explaining threading, but I can't find anything that tells me how to check when a thread is completed or cancelled.

Can someone PLEASE help me out with this.......

Thanks a lot,

Bill......... 


Answer this question

Threading question

  • Chris Durkin

    Check out this article:  http://msdn.microsoft.com/library/default.asp url=/library/en-us/dnservice/html/service09032002.asp

    The "Calling the Control on the Right Thread" section is where the cancel/completed explanation begins. 

    Good luck! 

    -Diane

  • Threading question