how to use InvokeRequired

Richnl

Well-known member
Joined
Mar 20, 2007
Messages
93
Programming Experience
Beginner
Hi,
I am trying out some code from a book called network programming
For UDP messaging

The only thing is, I get a thread not safe error
The solution from F1 Help is not really applicable because it uses outdated code (SetTextCallback)
I know I need to call the same method again, but on the thread the control is created on.
It also mentioned delegates briefly, so I will look at that in the meantime

this is what I am dealling with know
VB.NET:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Label2.Text = CStr(Me.Handle)
        Dim thdUDPServer = New Thread(New ThreadStart(AddressOf ServerThread))
        thdUDPServer.start()
    End Sub


    Public Sub ServerThread()
        Dim udpClient As New UdpClient(8080)
        While True
            Dim RemoteIpEndPoint As New IPEndPoint(IPAddress.Any, 0)
            Dim receiveBytes As Byte()
            receiveBytes = udpClient.Receive(RemoteIpEndPoint)
            Dim ReturnData As String = Encoding.ASCII.GetString(receiveBytes)
            If (Me.lbConnections.InvokeRequired) Then
[COLOR="SeaGreen"]' need to do the right stuff here[/COLOR]
                Dim d As New ContextCallback(AddressOf ???)
                Me.Invoke(d, New Object)

            Else

                lbConnections.Items.Add(RemoteIpEndPoint.Address.ToString() + ":" + ReturnData.ToString)
            End If
        End While

    End Sub
 
Last edited:
VB.NET:
Private Delegate Sub dlgUpdateUI(ByVal text As String)
 
Sub updateUI(ByVal text As String)
    If theControl.InvokeRequired = True Then
        Dim d As New dlgUpdateUI(AddressOf updateUI)
        theControl.Invoke(d, text)
    Else
        theControl.Items.Add(text)
    End If
End Sub
 
thanks, John

so, I would just make a call in sub ServerThread() to Sub updateUI........
I thought I had to make a separate class with the delegate statement in it
For clarity:
Delegate statements can be put anywhere..
If they are placed in a separate class, they have to be declared public in order to get acces to them from calls outside the class.


Is there any limitation to my procedure, or what are the boundries?
Do you have a short explanation on when beginEnvoke/endEnvoke is used?
I have seen this used also in combination with async operations, but more with separate classes (is there a connection)

I have a similar problem with this one, but no delegate here
Could you take a look at it , it can help me to better understand async
VB.NET:
Dim fs As FileStream
    Dim Callback As AsyncCallback
    Dim Contents As Byte()

    Private Sub fs_StateChanged(ByVal asyncResult As IAsyncResult)

        If asyncResult.IsCompleted Then
            tbResult.Text = Encoding.UTF8.GetString(contents)
            fs.Close()
        End If
    End Sub


    Private Sub knpAsync_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles knpAsync.Click

        Me.OpenFileDialog.ShowDialog()
        Me.Callback = New AsyncCallback(AddressOf fs_StateChanged)
        fs = New FileStream(OpenFileDialog.FileName, FileMode.Open, FileAccess.Read, FileShare.Read _
        , 4096, True)
        ReDim Contents(fs.Length)
        fs.BeginRead(Contents, 0, fs.Length, Callback, Nothing)
    End Sub
 
Last edited:
EndInvoke is used to retrieve the return value if the delegate method is a function method. It has been debated if this call is required for every BeginInvoke.

For your FileStream.BeginRead you must call EndRead, this will also return you how many bytes was actually read, though I don't think it will differ from the BeginRead request, and you don't need this value if you requested all bytes.
 
For your FileStream.BeginRead you must call EndRead, this will also return you how many bytes was actually read, though I don't think it will differ from the BeginRead request, and you don't need this value if you requested all bytes.

How exactly will this work?

Just FileStream.EndRead..? or did you say add it before begin read
what exactly can I leave out there ...?
 
'fs' is the FileStream instance you started BeginRead on, and this is what you have to EndRead.
 
Back
Top