Results 1 to 4 of 4

Thread: [RESOLVED] Delegate Problem

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Resolved [RESOLVED] Delegate Problem

    Creating a simple program to monitor changes to Network Adapter(s) status, vis:
    Code:
    Imports System
    Imports System.Net
    Imports System.Net.NetworkInformation
    Public Class Form1
    
        Private Delegate Sub DoResults(ByVal nName As String, ByVal nStatus As String)
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            AddHandler NetworkChange.NetworkAddressChanged, AddressOf AddressChangedCallback
            TextBox1.Text = "Monitoring for changes"
            TextBox1.AppendText(vbNewLine)
        End Sub
    
        Private Sub UpdateText(ByVal AdapterName As String, ByVal AdapterStatus As String)
            TextBox1.AppendText(AdapterName)
            TextBox1.AppendText(" ")
            TextBox1.AppendText(AdapterStatus)
            TextBox1.AppendText(vbNewLine)
        End Sub
    
        Private Sub AddressChangedCallback(ByVal sender As Object, ByVal e As EventArgs)
            Dim adapters As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
            Dim n As NetworkInterface
            For Each n In adapters
                Dim adResult As New DoResults(AddressOf UpdateText)
                Try
                    adResult.Invoke(n.Name.ToString, n.OperationalStatus.ToString)
                Catch ex As Exception
                    MessageBox.Show("Exception: " & ex.Message)
                End Try
            Next n
        End Sub
    End Class
    I'm getting an "Cross-thread operation not valid: Control Textbox1 accessed from a thread other than the thread it was created on" exception when I invoke the Delegate. Clearly there's a gap in my understanding; can someone perhaps explain what I ought to be doing ?

  2. #2

    Thread Starter
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Delegate Problem

    I appear to have resolved this
    Code:
        Private Sub AddressChangedCallback(ByVal sender As Object, ByVal e As EventArgs)
            Dim adapters As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
            Dim n As NetworkInterface
            For Each n In adapters
                Dim adResult As New DoResults(AddressOf UpdateText)
                Try
                    Me.Invoke(adResult, n.Name.ToString, n.OperationalStatus.ToString)
                Catch ex As Exception
                    MessageBox.Show("Exception: " & ex.Message)
                End Try
            Next n
        End Sub
    whether it's the 'best' way or not I don't know

  3. #3
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: [RESOLVED] Delegate Problem

    No need to write custom delegates. Use the Action(Of T delegate or the MethodInvoker delegate. Side note Use Environment.NewLine over VbNewline. It's a is a string containing \r\n which are escape characters for return and new line vbNewLine is char 10 and char 13. The runtime decides which one to used based on the platform its being run on.

    vb Code:
    1. Imports System
    2. Imports System.Net
    3. Imports System.Net.NetworkInformation
    4.  
    5. Public Class Form1
    6.  
    7.     Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    8.         AddHandler NetworkChange.NetworkAddressChanged, AddressOf AddressChangedCallback
    9.         Me.TextBox1.Text = "Monitoring for changes"
    10.         Me.TextBox1.AppendText(Environment.NewLine)
    11.     End Sub
    12.  
    13.     Private Sub UpdateText(ByVal AdapterName As String, ByVal AdapterStatus As String)
    14.         Me.TextBox1.AppendText(String.Format("{0} {1}{2}",
    15.                                              AdapterName,
    16.                                              AdapterStatus,
    17.                                              Environment.NewLine))
    18.     End Sub
    19.  
    20.     Private Sub AddressChangedCallback(ByVal sender As Object, ByVal e As EventArgs)
    21.         Dim adapters As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
    22.         Dim n As NetworkInterface
    23.         For Each n In adapters
    24.             Try
    25.                 Me.Invoke(New MethodInvoker(Sub() UpdateText(Name.ToString, n.OperationalStatus.ToString)))
    26.             Catch ex As Exception
    27.                 MessageBox.Show("Exception: " & ex.Message)
    28.             End Try
    29.         Next n
    30.     End Sub
    31. End Class
    My Github - 1d3nt

  4. #4
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: [RESOLVED] Delegate Problem

    You could get rid of UpdateStatus altogether:-
    vbnet Code:
    1. '
    2.     Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    3.         AddHandler NetworkChange.NetworkAddressChanged, AddressOf AddressChangedCallback
    4.  
    5.         TextBox1.Text = "Monitoring for changes"
    6.         TextBox1.AppendText(Environment.NewLine)
    7.     End Sub
    8.  
    9.     Private Sub AddressChangedCallback(sender As Object, e As EventArgs)
    10.  
    11.         Dim adapters As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
    12.         Dim n As NetworkInterface
    13.         For Each n In adapters
    14.  
    15.             Try
    16.                 TextBox1.Invoke(Sub()
    17.                                     TextBox1.AppendText(n.Name & " " & n.OperationalStatus.ToString & Environment.NewLine)
    18.                                 End Sub)
    19.  
    20.             Catch ex As Exception
    21.                 MessageBox.Show("Exception: " & ex.Message)
    22.             End Try
    23.         Next n
    24.     End Sub

    You could even get rid of the AddressChangedCallback sub too if you wanted to get really terse.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

Posting Permissions

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



Click Here to Expand Forum to Full Width