Question on Threading...

(VB.NET) So I finally understand how it works, incorporated on one of my long processes and it works.  Great.  But now when I run the process (showing a progress bar) I can only do other tasks after the progressbar finishes.  The Setup:

I have a subroutine that creates about 5 excel spreadsheet from records in a database.  The progressbar shows for each file creation.  As it's creating a file I can't access my Menu (File, View,etc..) until a file is done.  The menu item opens but the process stops.  After I release the menu item, the process continues.

The threading code is as follows:

Private
Sub ExcelButton_Click(ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles PictureBox4.Click

   Dim t As New System.Threading.Thread(AddressOf StartProcess)
   t.IsBackground =
True
   t.Start()

End Sub

Private Sub StartProcess()
   Try

      Dim mi As New MethodInvoker(AddressOf BuildExcelFiles)
      Cursor.Current = Cursors.WaitCursor

      BeginInvoke(mi)

   Catch ex As Threading.ThreadInterruptedException

      MessageBox.Show("Friendly Message : An error has occured. _
         Retry or call I.T. Support." & vbCrLf & vbCrLf & "Nasty _
         Message :" ex.Message, "Error", MessageBoxButtons.OK, _
         MessageBoxIcon.Error)

   Finally

      Cursor.Current = Cursors.Default

   End Try

End Sub

Is there anything in particular that I need to do differently

Thanks in advance




Answer this question

Question on Threading...

  • Cleber Dantas

    That didn't work because the process isn't a delegate. (That's the error I received)...

  • Worachart

    The PROCESS itself is not the delegate. You need to create a delegate whose task is simply to modify the current value on the progress bar. You should not touch any graphical controls from any thread except the one which created them, ie the GUI thread. So invoke your progress bar delegate from the processing thread.

  • Cosmin Paun

    MethodInvoker is (almost) a regular delegate to a function that takes no arguments. Therefore it won't match The function that takes Some arguments. In addition VB (I think) doesn't support anonymous delegates therefore you would have to create your own function that does that.

    Private Sub MyAddNode()
      TreeView1.Nodes.Add New TreeNode("Excel File Creation")
    End Sub


    And then in your code you can say
    BeginInvoke(New MethodInvoker(AddressOf MyAddNode))

  • Tiago_mor

    Thank you guys so much.  Learning something new everyday, cool.

  • Andrew Kidd

    Ok,

    Here's a bit of the BuildExcelFiles procedure and how the Progressbar is updated.  For each file created, the same typical loop is ran.

    ProgressBar1.Minimum = 0
    ProgressBar1.Maximum = datatable.Rows.Count
    ProgressBar1.Value = 0
    ProgressBar1.Step = 1

    'build my datatable with data for the excel spreadsheet, then open excel object...

    for i = 0 to datatable.rows.count - 1
       cell(i+1,1) = datatable.rows(i).item(0)
       cell(i+1,2) = datatable.rows(i).item(1)
       'and so on....
     
       ProgressBar1.PerformStep()
    next

    When I call the BuildExcelFiles, only, in the StartProcess procedure, I get the error message about the control.begininvoke needs to be there, blah blah blah.

    Any other thoughts


  • mikelaw

    Awesome, that worked but one more issue, the Treeview control is doing the same thing on the Add method.  Here's the line I attempted after your help but the AddressOf doesn't like the parenthesis' :

    BeginInvoke(New MethodInvoker(AddressOf TreeView1.Nodes.Add(New TreeNode("Excel File Creation"))))

    Anything different about the Treeview



  • hazzoom

    From your snippet (assming that BuildExcelFiles does all the work)
    You are executing BuildExcelFiles back on your GUI thread!
    BeginInvoke will switch the thread and there is no purpose to your creating a new thread.

    You should call BuildExcelFiles directly from StartProcess and only to update the progress bar you would call BeginInvoke


  • AviF

    Sorry that was C# syntax
    I think this would be correct VB syntax
    New MethodInvoker(AddressOf ProgressBar1.PerformStep)

  • Michaelxyz3

    Call BuildExcelFiles in the StartProcess and call
    BeginInvoke(new MethodInvoker(ProgressBar1.PerformStep));

  • Question on Threading...