help invoking actions vb.net

Hello Experts, I created a multithreaded applications using ms visual studio 2008.  It almost seems randomly that my program will sit and wait while I am trying to invoke in action.  I am not sure if I am using the best method.  If i should be using a begininvoke or invoke.  Could someone look at my code, i am basically updating some progress bars and labels while the heavy processing is working.
Sub start_progress()
        If Me.InvokeRequired Then
            Dim dele As Action
            dele = AddressOf start_progress
            Me.Invoke(dele)
            disable_timers(False)
        Else
            If record_loading_status = False Then
                update_cylinder_progress()
                ts_main_bar.Style = ProgressBarStyle.Blocks
                ts_main_status.Text = "Ready!"
                If tsbrefresh.Enabled = False Then
                    tsbrefresh.Enabled = True
                End If
                Me.Refresh()
                disable_timers(True)
                Application.DoEvents()
            Else
                ora_conn = New OracleConnection("database location")
                tsbrefresh.Enabled = False
                ts_main_status.Text = "Loading Records"
                ts_main_bar.Style = ProgressBarStyle.Marquee
                clear_controls(Me)
                Me.Refresh()
            End If
        End If
    End Sub

    Sub stop_progress()
        If Me.InvokeRequired Then
            record_loading_status = False
            Dim dele As Action
            dele = AddressOf start_progress
            '  Me.Invoke(dele)  'Should i use invoke or begin invoke, it seems like my program locks up here.
            Me.BeginInvoke(dele)
        End If
    End Sub

Open in new window

tentavariousAsked:
Who is Participating?
 
kdwoodConnect With a Mentor Commented:
Are you sure your problem isn't further upstream in your code?

Where and when is start_progress and stop_progress being called?

How about your other functions/subs like clear_controls(), disable_timers(), update_cylinder_progress() . . . are they interacting with form controls that were initialized in another thread?  If so, should they be testing to see if InvokeRequired also?

I'm not great with threading, but just throwing out some ideas.

Regards,

Keith
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Yes...Invoke() is synchronous while BeginInvoke() is asynchronous.

If Invoke() is locking up your flow then switch to BeginInvoke().
0
 
tentavariousAuthor Commented:
Ok, is there another line of code in which i can check the progress of the begininvoke?  And if so can i get an example?
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Do you want to know when it is complete?...or something else?
0
 
tentavariousAuthor Commented:
yes, when it is complete would be ideal
0
 
tentavariousAuthor Commented:
I would like to but it in while loop and check the status, it seems like the begininvoke didnt fix the problem and my program occasionally locks up.
0
 
kdwoodCommented:
tentavarous,

First my disclaimer:  I won't even pretend to be anywhere near Idle_Minds level on this stuff.  In fact, he has helped me out many times in the past.  

I happen to have a project with a lot of background workers and threads that updates stock prices, candlestick charts and a lot of other dynamic data.   Right or wrong, I used the approach of creating Delegates and invoking the delegates where necesarry.   It looks like you are doing essentially the same thing, just coded a little differently.   Here is a snippet of my code for what it's worth:


Private Delegate Sub ClearTradeRowsDLG(ByVal myDG As DataGridView)

   Public Sub ClearTradeRows(ByVal myDG As DataGridView)

        If Me.InvokeRequired Then
            Me.Invoke(New ClearTradeRowsDLG(AddressOf ClearTradeRows), New Object() {myDG})
        Else
            myDG.Rows.Clear()
        End If

    End Sub

Open in new window

0
 
tentavariousAuthor Commented:
I looks like your making a synchronous call back.  I thought that may be my problem, so i tried using begininvoke instead, which should create a asynchronous call back.  The code below is my latest attempt at getting it to work, not sure if its the correct solution.


Sub stop_progress()
        If Me.InvokeRequired Then
            Dim result As System.IAsyncResult
            Dim dele As Action
            dele = AddressOf start_progress
            result = Me.BeginInvoke(dele)
            Me.EndInvoke(result)
        End If
    End Sub

Open in new window

0
 
tentavariousAuthor Commented:
Ok, my program is still locking up,  I put a break on the stop_progress, and sometimes it just waits and nothing happens.  I end up having to kill the program.  Any idea on what may be causing it to lock?
Sub stop_progress()

   If Me.InvokeRequired Then
            Dim result As System.IAsyncResult
            Dim dele As Action
            dele = AddressOf start_progress
            result = Me.BeginInvoke(dele)
            Me.EndInvoke(result)
        End If
end Sub
0
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
See EndInvoke():
http://msdn.microsoft.com/en-us/library/2e08f6yc(VS.71).aspx

The EndInvoke method is used to retrieve the results of the asynchronous call. It can be called any time after BeginInvoke; if the asynchronous call has not completed, EndInvoke will block until it completes.

So calling EndInvoke() immediately after BeginInvoke() makes it essentially synchronous operation and equivalent to using Invoke() in the first place.

The link above outlines several options for determining when the async operation has completed.  Hint...don't use EndInvoke() from the main UI until AFTER you are sure the operation is complete.

I recommend you look at the section entitle "Executing a Callback Method When an Asynchronous Call Completes".
0
All Courses

From novice to tech pro — start learning today.