Solved

Using Delegates for Cross-thread operations

Posted on 2008-10-13
2
491 Views
Last Modified: 2012-06-21
I need some help understanding the use of Delegates. I would like to have a listbox on the main form and be able to add to it from other threads. Below is some example code that I've been playing with but not very successfully. Can someone point out what I'm doing wrong? I keep getting "Cross-thread operation not valid". Thanks!
Public Class One

    Private Delegate Sub Logger(ByVal _message As String)

    Dim t As Threading.Thread

    Dim WithEvents oTwo As New Two
 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        t = New Threading.Thread(AddressOf oTwo.startCounting)

        t.Start()

    End Sub

    Private Sub updateList(ByVal entry As String)

        ListBox1.Items.Add(entry)

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        t.Abort()

    End Sub

    Sub ThreadLogging(ByVal entry As String) Handles oTwo.LogUpdated

        Invoke(New Logger(AddressOf updateList), entry)

        Dim writer As Logger

        writer = AddressOf updateList

        writer.Invoke(entry)

    End Sub

End Class
 

Public Class Two

    Public Event LogUpdated(ByVal entry As String)
 

    Public Sub startCounting()

        Dim count As Integer = 0

        While True

            WriteMonitorLog(count.ToString)

            count += 1

            Threading.Thread.Sleep(100)

        End While

    End Sub

    Private Sub WriteMonitorLog(ByVal entry As String)

        RaiseEvent LogUpdated(entry)

    End Sub

End Class

Open in new window

0
Comment
Question by:Tharo_Systems
2 Comments
 
LVL 13

Accepted Solution

by:
crazyman earned 500 total points
ID: 22703457
change this

Sub ThreadLogging(ByVal entry As String) Handles oTwo.LogUpdated
        Invoke(New Logger(AddressOf updateList), entry)
            End Sub
0
 

Author Comment

by:Tharo_Systems
ID: 22703501
I knew it had to be something simple! Thanks so much!
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now