Click here to Skip to main content
13,046,045 members (45,772 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hello im new to this site so forgive me if I have broken any rules here...
I am writing a test application to aid me in the Threading class
how ever when i invoke (for thread safety) the process actualy runs on the same thread, I have looked everywhere on line for an answer but no dice...
My source code is as follows...
Imports System.Threading
Public Class Form
    Private pass As Integer = 0
    Private Delegate Sub CallBack(ByVal No As Integer)
    Public Sub New()
        Me.Text = "MultiThreading - Main Thread ID: " & Thread.CurrentThread.ManagedThreadId
    End Sub
    Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button.Click
        Dim t As Thread = New Thread(AddressOf AddItem, 0)
        t.IsBackground = True
        pass = pass + 1
    End Sub
    Private Sub AddItem(ByVal ItemNo As Integer)
        If ListBox.InvokeRequired = True Then
            ListBox.BeginInvoke(New CallBack(AddressOf AddItem), ItemNo)
            ListBox.Items.Add("Item Number " & ItemNo & " - Added by Thread ID: " & Thread.CurrentThread.ManagedThreadId)
        End If
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim t As Thread = New Thread(AddressOf MSGTest)
        t.Start("Test Message")
    End Sub
    Private Sub MSGTest(ByVal MSG As String)
        MsgBox(MSG & "...   MsgBox Generated by Thread ID: " & Thread.CurrentThread.ManagedThreadId, MsgBoxStyle.Information, "Thread Test")
    End Sub
End Class

The test function works but that does not use any invoke,
Any help would be great as I have spent some time working this one out.

Thanks in advanced... Blingking01
Posted 10-Sep-10 13:47pm
Blingking01 25-Sep-10 14:28pm
Sorry for the confusing question,
Am I to assume that I dont need to invoke (The ListBox.BeginInvoke(New CallBack(AddressOf AddItem), ItemNo)) but instead use Synclock on any resource that Im worried that another thread will call at the same time?

Thank you for your response

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You're holding it the wrong way. ;)

Threads are in processes, not the other way around, so it's hard to be sure what you're asking (have you confused the question, the concept or the helper?).

Here's a CP starter project on threading in VB.NET:
Introduction to making multithreaded VB.NET Apps[^]

So, if you've read through the above and you're still thinking it's not working, perhaps clarify your question a little as to what you expect, exactly what you get (including any errors) and we'll see what we can do.

Dalek Dave 15-Sep-10 10:00am
Good Answer.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170713.1 | Last Updated 15 Sep 2010
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100