[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2130
  • Last Modified:

VB.NET WinForm Timer and Cross Threaded Control Update using Delegate

I have a vb.net 2.0 winform app that I am working on. This app has a timer that fires every 1 second. I want it to then update a control on the form with a particular value. Since I cannot update controls cross thread I am trying to use a delegate to do this. But it is still telling me I can't update cross thread. I have delcared the delegate as:

    Public Delegate Sub UpdateUICallBack()

and the sub as:

    Private Sub PlayUpdateTimerFired(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)
        Dim del As UpdateUICallBack
        del = New UpdateUICallBack(AddressOf SetPlayProgress)
        del.Invoke()
    End Sub

and the sub it is trying to call as

    Private Sub SetPlayProgress()
        PlayProgress.Width = 142 * n
        Application.DoEvents()
    End Sub

I can't see anything wrong in this and have looked for what seems like forever at examples and documents about this and it looks right according to everything I've seen so far. I am at a loss. Can someone please help me with this? Thanks so much.
0
Donzella
Asked:
Donzella
  • 2
  • 2
  • 2
  • +1
2 Solutions
 
DonzellaAuthor Commented:
Well, I found a solution on another question here at EE where Idle_Mind had answered it and I changed my code to the follow which ended up working for me. So if Idle_Mind shows up here and says something I will give you the points. :)

    Private Delegate Sub StatusDelegate(ByVal status As Double)


    Private Sub SetPlayProgress(ByVal n As Double)
        If PlayProgress.InvokeRequired Then
            Dim dg As New StatusDelegate(AddressOf SetPlayProgress)
            PlayProgress.Invoke(dg, New Object() {n})
        Else
            PlayProgress.Width = 142 * n
        End If
    End Sub

    Private Sub PlayUpdateTimerFired(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)
        SetPlayProgress(n)
    End Sub
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Thanx!...but you should request a PAQ/Refund in Community Support instead...  ;)
0
 
drichardsCommented:
Another solution would be to use a System.Windows.Forms.Timer which executes its callbacks on the main form thread.  Then you don't have this issue.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
gregoryyoungCommented:
since its a windows forms app why not just use a System.Windows.Forms timer which runs by passing a message to your main thread (no need to invoke).

Cheers,

Greg
0
 
DonzellaAuthor Commented:
windows.forms.timer seems to have a different sort of setup. I guess I will have to dig into it a little bit. I didn't know such an animal existed.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Windows.Forms.Timer is the one that should appear right in your existing ToolBox along with all the other controls!...
0
 
drichardsCommented:
And after you drop a timer from the Toolbox onto your form, double click the "Tick" event in the Properties window, and copy your timer handler code into the resulting method.  Then start and stop the timer at appropriate points in your app.

Only difference between the Tick handler and the Elapsed handler you are using now is the ElapsedEventArgs parameter changes to simply EventArgs (EventHandler delegate vs. ElapsedEventHandler delegate).
0

Featured Post

Technology Partners: 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!

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now