threading question

wsyeager

Centurion
Joined
Apr 10, 2003
Messages
140
Location
Weston, FL
I am able to successfully execute a thread. However, I notice that when the thread is executing, the hourglass is off (which is expected), but I can't seem to select any other menu items in my MDI application while the thread is executing. I can do other stuff with Windows outside of my application, but not from within my MDI app. Why???

I have another process in my MDI app that uses threading (where I don't need to create a delegate and I can access other menu items in my MDI app while the thread is executing).

Here is my code in regards to the first item I talked about above where I had to create a delegate (1st paragraph):
Visual Basic:
Private thdPendingRpts As Thread
.
.
.

    Private Sub btnRpt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRpt.Click 

        Try 
            thdPendingRpts = New Thread(New ThreadStart(AddressOf ThreadProc)) 
            thdPendingRpts.IsBackground = True 
            thdPendingRpts.Priority = ThreadPriority.AboveNormal 
            thdPendingRpts.Name = "Pending Reports" 
            thdPendingRpts.Start() 
            StatusBar1.Text = "Thread 'Pending Reports' has started" 
        Catch exException As ThreadInterruptedException 
            StatusBar1.Text = exException.Message 
        Catch exException As ThreadStateException 
            StatusBar1.Text = exException.Message 
        Catch exException As ThreadAbortException 
            StatusBar1.Text = exException.Message 
        Catch exException As System.Exception 
            StatusBar1.Text = exException.Message 
        End Try 

    End Sub 

    Private Sub ThreadProc() 

        Try 
            Dim mi As New MethodInvoker(AddressOf ProcessData) 
            Me.Invoke(mi) 
        Catch exException As ThreadInterruptedException 
            StatusBar1.Text = exException.Message 
        Catch exException As ThreadStateException 
            StatusBar1.Text = exException.Message 
        Catch exException As ThreadAbortException 
            StatusBar1.Text = exException.Message 
        Catch exException As System.Exception 
            StatusBar1.Text = exException.Message 
        End Try 

    End Sub 

    Private Sub ProcessData() 

        btnRpt.Enabled = False 

        StatusBar1.Text = "Retrieving SQL1 data..." 

        DsOrdStatDlyRptPend1.Clear() 

        BuildSQL1() 
        If Not blnError Then 
            For i = 0 To DsOrdStatDlyRptPend1.OrdStatDlyRptPendSQL1.Rows.Count - 1 
                StatusBar1.Text = "Retrieving SQL2 data..." 
                BuildSQL2() 
            Next 
            If Not blnError Then 
                For i = 0 To DsOrdStatDlyRptPend1.OrdStatDlyRptPendSQL1.Rows.Count - 1 
                    StatusBar1.Text = "Retrieving SQL3 data..." 
                    BuildSQL3() 
                Next 
                If Not blnError Then 
                    For i = 0 To DsOrdStatDlyRptPend1.OrdStatDlyRptPendSQL1.Rows.Count - 1 
                        StatusBar1.Text = "Retrieving SQL4 data..." 
                        BuildSQL4() 
                    Next 
                    If Not blnError Then 
                        StatusBar1.Text = "Generating report..." 
                        BuildReport() 
                    End If 
                End If 
            End If 
        End If 

        StatusBar1.Text = "Thread 'Pending Reports' has finished" 
        btnRpt.Enabled = True 

        thdPendingRpts = Nothing 

    End Sub
Does any body know why I can't select any other items from within my application while this thread is executing???
 
Last edited by a moderator:
You should use the Form's Invoke method to marshal to the correct (UI) thread before running code that updates any controls. The only thread safe call on a control that isn't for invoking delegates is Invalidate().

The correct prodecure for doing this is to first check the control's InvokeRequired property. If this returns true, use the control's Invoke method to run your procedure, if it returns false just run the procedure normally.
 
Back
Top