locked
begininvoke and invokerequired usage

    Question

  • instead of using if control.invoke required in a procedure can I just call the procedure with begininvoke?

    I have, possibly, the following:

     Private Sub UpDateTimer_Elapsed(sender As Object, e As System.Timers.ElapsedEventArgs) Handles UpDateTimer.Elapsed
            UpdateTime()
            If MicActive Then
                PTTCancelTime += 1
                If PTTCancelTime < SpeechTime Then
                    BeginInvoke(New SetPTTTime(AddressOf DoSetPTTTime), New Object() {PTTCancelTime})
                Else
                    MicActive = False
                    PTTTrue = False
                    PTTTimeOut()
                End If
            End If
        End Sub
    
      Private Delegate Sub SetPTTTime(value As Integer)
    
    
    Private Sub DoSetPTTTime(ByVal value As Integer)
    
                With SpeechProgress
                    .Value = value
                    .Text = "Speech Time Left = " & SpeechTime - value & " secs."
                End With
    
        End Sub
    

    or do i have to use the following:

     Private Sub UpDateTimer_Elapsed(sender As Object, e As System.Timers.ElapsedEventArgs) Handles UpDateTimer.Elapsed
            UpdateTime()
            If MicActive Then
                PTTCancelTime += 1
                If PTTCancelTime < SpeechTime Then
                    DoSetPTTTime(PTTCancelTime)
                Else
                    MicActive = False
                    PTTTrue = False
                    PTTTimeOut()
                End If
            End If
        End Sub
    
      Private Delegate Sub SetPTTTime(value As Integer)
        Private Sub DoSetPTTTime(ByVal value As Integer)
         
            If SpeechProgress.InvokeRequired Then
                Dim d As New SetPTTTime(AddressOf DoSetPTTTime)
                Me.Invoke(d, New Object() {value})
            Else
                With SpeechProgress
                    .Value = value
                    .Text = "Speech Time Left = " & SpeechTime - value & " secs."
                End With
            End If
    
        End Sub
    

    is there any advantage of one over the other?

    Both methods seem to work OK.


    Jim Bartlett
    Thursday, December 01, 2011 10:30 AM

Answers

  • "but which is more correct or quicker?"

    The one that does what you want to accomplish.  Do you want to send a message or post a message.

    • Proposed as answer by Mike FengModerator Friday, December 02, 2011 11:46 AM
    • Marked as answer by JimmyT Friday, December 02, 2011 1:00 PM
    Thursday, December 01, 2011 2:24 PM

All replies

  • The synchronous call is less error prone.  Variables passed in the asynchronous call can be accessed by both threads at the same time.
    Thursday, December 01, 2011 1:12 PM
  • That is not a problem as I use the default byval when passing data so even if one thread updates a variable the other thread will not notice. (I think!!!) It would not realy matter if it did update anyway.

    but which is more correct or quicker?

     


    Jim Bartlett
    Thursday, December 01, 2011 1:46 PM
  • "but which is more correct or quicker?"

    The one that does what you want to accomplish.  Do you want to send a message or post a message.

    • Proposed as answer by Mike FengModerator Friday, December 02, 2011 11:46 AM
    • Marked as answer by JimmyT Friday, December 02, 2011 1:00 PM
    Thursday, December 01, 2011 2:24 PM