Solved

help invoking actions vb.net

Posted on 2011-03-01
10
330 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:tentavarious
  • 5
  • 3
  • 2
10 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 35009109
Yes...Invoke() is synchronous while BeginInvoke() is asynchronous.

If Invoke() is locking up your flow then switch to BeginInvoke().
0
 

Author Comment

by:tentavarious
ID: 35009367
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 35009421
Do you want to know when it is complete?...or something else?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:tentavarious
ID: 35010610
yes, when it is complete would be ideal
0
 

Author Comment

by:tentavarious
ID: 35017701
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
 
LVL 10

Expert Comment

by:kdwood
ID: 35017969
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
 

Author Comment

by:tentavarious
ID: 35018063
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
 

Author Comment

by:tentavarious
ID: 35029580
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
 
LVL 10

Accepted Solution

by:
kdwood earned 175 total points
ID: 35029781
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
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 75 total points
ID: 35030085
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

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question