Click here to Skip to main content
15,892,005 members

Multithreading in VB.Net

Blingking01 asked:

Open original thread
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
Tags: Visual Basic, Threads

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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