invoking not working correctly

Waldo2k2

Member
Joined
Feb 6, 2006
Messages
7
Programming Experience
5-10
I'm having some trouble with invoking and delegates. The basic problem is, I just upgraded a project to VS 2005, and now I come to find I was doing some things illegally in my code. I've been calling the progressbar.performstep() method from within a thread that isn't the UI thread. I've looked at every tutorial on delegates and invoking that I can find, but what I've done so far still causes the application to error. Here's the basic idea of what I'm doing
VB.NET:
Expand Collapse Copy
'using an integer as an index to what progressbar i want to update
'was just passing the name of the progressbar but wanted to avoid
'any conflicts that may have been causing between threads
Private Delegate Sub pbarDelegate(ByVal index As Integer)


private sub callThread()
   theThread.Start()
end sub

private sub insideThread()
   'this is what runs when the thread starts
   Dim cobarDelegate As pbarDelegate
   cobarDelegate = New pbarDelegate(AddressOf dostep)

   cobarDelegate.Invoke(1)
end sub

Private Sub dostep(ByVal index As Integer)
        Select Case index
            Case 1
                'error is of course right here
                cobar.PerformStep()
            Case 2
                adbar.PerformStep()
            Case 3
                embar.PerformStep()
            Case 4
                prbar.PerformStep()
        End Select
End Sub
This of course is a run-time error. I can't figure out why it's not invoking onto the UI thread, any ideas?
 
::update::
got everything thread-safe, there's a problem now. I don't know if it's the invoking causing it or what (I'm using beginInvoke for everything btw), but all the controls respond much slower than they did in .net 1.1 (prior to being thread-safe) and the progressbars don't respond at all. However, if I press the button that launches my threads a second time (after they've completed) the progress bars respond (albeit jumpy). Here's a rundown of what I have right now:
VB.NET:
Expand Collapse Copy
Public Class frmMain
    Inherits System.Windows.Forms.Form

    Private Delegate Sub stepBars(ByVal bar As ProgressBar)
    Dim barStep As stepBars
...
'inside thread
    barStep = New stepBars(AddressOf progressBarStep)
    cobar.BeginInvoke(barStep, cobar) 'where cobar is a progressbar control
...
'progressBarStep sub which is invoked
    Private Sub progressBarStep(ByVal bar As ProgressBar)
        bar.PerformStep()
    End Sub
I have 3 threads running concurrently (besides the UI thread), each with their own progress bar. None work correctly. They also all update a label control (thread-safely of course). The label jumps around a bit at first, then updates smoothly after one of the threads completes. It doesn't make sense that this would all work fine in msvs 2003 with .net 1.1, but not in msvs 2005 with .net 2.0. Besides making it thread-safe there shouldn't be such a huge performance difference (if anything porting it to 2.0 should have made it faster).
I really need to get some feedback on this, so if anyone has the time any insight would be appreciated. This is a work project and I'm the only developer on the project, and the only one who works with .net.
 
Last edited:
::resolved::
well, I figured out the problem and decided to post it for future reference.
Basically, if you invoke a function and pass an object as the parameter, it does execute it on the UI thread, but it doesn't have access to the objects properties. So the minimum and maximum for the progressbars were not set, and it wasn't throwing an exception, I got lucky and saw it in the debug window. So if you ever work with a form control by passing it byval as an object, you must set it's properties in that function before using it.
VB.NET:
Expand Collapse Copy
private sub progressBarStep(byval bar as progressBar)
    bar.maximum=intMax
    bar.minimum=intMin
    bar.doStep()
end sub
 
Back
Top