Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I would like to update a listbox control on my form from a thread I created.

I am using a thread to listen to port 514 which is the syslog port.

When there is data to receive the thread then exits and returns the data.
I would like to trigger an event on my main form that will then update the listbox and recall the thread to listen for more data.

I am modifying code that just printed to the console and now I am trying to place it in a form.

What I have tried:

VB
Imports System.IO
Imports System.Net.Sockets
Imports System.Net
Imports System.Text
Imports System.Net.Http
Imports System.Threading


Public Class Form1

    Shared WithEvents mysyslog As syslog = New syslog()

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ListBox1.Items.Add("Syslog")

        Dim t As Thread
        t = New Thread(AddressOf mysyslog.Getline)
        t.Start()

    End Sub



    Shared Sub SyslogEvent(ByVal sdatareceived As String) Handles mysyslog.ThreadComplete

        Console.WriteLine(sdatareceived)


        'ListBox1.Items.Add(sdatareceived)  'When I add this line I get  ...cannnot refer to an instance member of a class from within a shared method or shared member initializer


        MsgBox("done")

    End Sub

End Class


Public Class syslog

    Dim ipeRemoteIpEndPoint As New IPEndPoint(IPAddress.Any, 0)
    Dim udpcUDPClient As New UdpClient(514)
    Public sDataRecieve As String
    Dim bBytesRecieved() As Byte
    Dim sFromIP As String

    Public Event ThreadComplete(ByVal sDataRecieve As String)

    Public Sub Getline()
        bBytesRecieved = udpcUDPClient.Receive(ipeRemoteIpEndPoint)
        sDataRecieve = Encoding.ASCII.GetString(bBytesRecieved)
        sFromIP = ipeRemoteIpEndPoint.Address.ToString
        RaiseEvent ThreadComplete(sFromIP & sDataRecieve)
    End Sub

End Class
Posted
Updated 13-Nov-16 19:55pm
v2

1 solution

There are two problems here a use you haven't met one of them yet, because the other prevents it occurring.
The problem you have met is simple: you cannot access class instance fields, properties, methods, or events within a Shared method because it (by definition) does not have an instance associated with it.
That is what the error is saying: a Shared method does not have any interface to work with.
You may be able to get round this by removing the Shared attribute from the method.

The problem it "hides" is that you cannot access UI controls from a thread other than the UI thread it was created on - and if you try you will get an error at run time: a "cross thread exception".
To cure that, you will be Invoke the control to move the update back to the UI thread: Control.Invoke Method (Delegate) (System.Windows.Forms)[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900