Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Threading VB.NET
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 10-Sep-10 14:47pm
Comments
Blingking01 at 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
good
Please Sign up or sign in to vote.

Solution 1

You're holding it the wrong way. Wink | ;)
 
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.
  Permalink  
Comments
Dalek Dave at 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
0 George Jonsson 215
1 Kornfeld Eliyahu Peter 169
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 15 Sep 2010
Copyright © CodeProject, 1999-2014
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