Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
1.00/5 (1 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()
        InitializeComponent()
        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.GetHashCode()
        t.SetApartmentState(ApartmentState.MTA)
        t.IsBackground = True
        t.Start(pass)
        pass = pass + 1
    End Sub

    Private Sub AddItem(ByVal ItemNo As Integer)
        If ListBox.InvokeRequired = True Then
            ListBox.BeginInvoke(New CallBack(AddressOf AddItem), ItemNo)
        Else
            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
Comments
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

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.

Cheers.
 
Share this answer
 
Comments
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)



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