Solved

help invoking actions vb.net

Posted on 2011-03-01
10
322 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Please explain: Aspect Oriented Programming 2 57
Set form below another form 3 26
Showdialog 8 21
Different Delete Messages 7 23
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now