Solved

Using Delegates for Cross-thread operations

Posted on 2008-10-13
2
495 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Vb.net progressbar with timer1 in visual studio win app 11 51
Read top line from CSV file 14 60
Datatable / Dates ? 4 33
Need Help With GDI+ in VB.Net 8 44
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

710 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