Results 1 to 6 of 6

Thread: how to use InvokeRequired

  1. #1
    Richnl is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Mar 2007
    Posts
    93
    Reputation
    95

    how to use InvokeRequired

    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
    Code:
    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
    ' need to do the right stuff here
                    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 by Richnl; 05-26-2007 at 7:37 AM.

  2. #2
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    14,535
    Reputation
    2735
    Code:
    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

  3. #3
    Richnl is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Mar 2007
    Posts
    93
    Reputation
    95
    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
    Code:
    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 by Richnl; 05-26-2007 at 2:43 PM.

  4. #4
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    14,535
    Reputation
    2735
    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.

  5. #5
    Richnl is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Mar 2007
    Posts
    93
    Reputation
    95
    Quote Originally Posted by JohnH View Post
    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 ...?

  6. #6
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    14,535
    Reputation
    2735
    'fs' is the FileStream instance you started BeginRead on, and this is what you have to EndRead.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking