Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

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):

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???

Edited by Robby

Thanks,

 

Bill Yeager (MCP, BCIP)

Microsoft Certified Professional

Brainbench Certified Internet Professional, .Net Programmer, Computer Programmer

YeagerTech Consulting, Inc.

  • *Gurus*
Posted

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.

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...