Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Visual Basic .NET Multithreading

Posted on 2010-11-20
10
Medium Priority
?
434 Views
Last Modified: 2013-11-26
Hello.

I have a problem with threads. In my program count of threads is user defined.
Threads working only once without completing the computation.  I don't know how to fix it..
Code + Screenshot attached.

Thanks.


Imports System.Threading

Public Class MultiThread
    Private Delegate Sub addToListBox_d(ByVal result As String)
    Private num As New Integer
    Private step1 As Integer = 1

    Private Sub addToListBox(ByVal result As String)
        If Me.InvokeRequired() Then
            Me.Invoke(New addToListBox_d(AddressOf addToListBox), New Object() {result})
        Else
            ListBox.Items.Add(result)
        End If
    End Sub

    Private Sub DoWork(ByVal state As Object)
        Dim txtToAdd As String
        Dim result As Integer = 0
        Dim counter As Integer = Val(state)
        Dim stop1 As Integer = Val(state) + step1
        Do While (counter < stop1)
            result += counter
            counter += 1
        Loop
        txtToAdd = state & " - " & result
        addToListBox(txtToAdd)
    End Sub

    Private Sub StartButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartButton.Click
        Dim counter As Integer = 1
        Do While (counter < Val(Threads.Text) + 1)
            Dim t As Thread = New Thread(AddressOf DoWork)
            t.IsBackground = True
            t.Start(counter)
            t.Join(counter)
            counter += step1
        Loop
    End Sub
End Class

Open in new window

screen.jpg
0
Comment
Question by:StolenMan
[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
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 34179876
What were you expecting to happen here?...that's some messy code to decipher...

Also, you have:

            t.Join(counter)

Which will only block for a mere couple of milliseconds and then move on.

Again, can you explain what the code is supposed to do?
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 34179937
What output are you expecting?
0
 
LVL 36

Expert Comment

by:Miguel Oz
ID: 34180969
Join the threads at the end not at creation time
Private Sub StartButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartButton.Click
        Dim counter As Integer = 1
        Dim result As New List(Of Thread)
        Do While (counter < Val(Threads.Text) + 1)
            Dim t As Thread = New Thread(AddressOf DoWork)
            t.IsBackground = True
            t.Start(counter)
            result.Add(t)
            counter += step1
        Loop
        For Each Item As Thread In result
            Item.Join()
        Next
    End Sub

Open in new window

0
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!

 

Author Comment

by:StolenMan
ID: 34181949
Idle_Mind, program does not continues computing. Thread creation not repeating.
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 34182939
Can you explain what the code is supposed to do and what should be the expected results?
0
 

Author Comment

by:StolenMan
ID: 34183101
I need any program with user defined count of threads (with GUI)... Very compact sample.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 34183518
How about something like this?

The form has a NumericUpDown, Button, and ListBox:
Imports System.ComponentModel
Public Class Form1

    Private R As New Random
    Private BackgroundWorkers As New List(Of BackgroundWorker)

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Static Counter As Integer

        ListBox1.Items.Clear()
        Button1.Enabled = False
        For i As Integer = 1 To NumericUpDown1.Value
            Dim bgw As New BackgroundWorker
            bgw.WorkerReportsProgress = True
            AddHandler bgw.DoWork, AddressOf bgw_DoWork
            AddHandler bgw.ProgressChanged, AddressOf bgw_ProgressChanged
            AddHandler bgw.RunWorkerCompleted, AddressOf bgw_RunWorkerCompleted
            BackgroundWorkers.Add(bgw)
            Counter = Counter + 1
            bgw.RunWorkerAsync(Counter)
        Next
    End Sub

    Private Sub bgw_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs)
        Dim ThreadID As Integer = e.Argument
        Dim bgw As BackgroundWorker = CType(sender, BackgroundWorker)
        For i As Integer = 1 To 10
            System.Threading.Thread.Sleep(R.Next(1000, 5001))
            bgw.ReportProgress(0, "ThreadID: " & ThreadID & " --> Progress: " & i)
        Next
    End Sub

    Private Sub bgw_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs)
        ListBox1.Items.Add(e.UserState.ToString)
    End Sub

    Private Sub bgw_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs)
        Dim bgw As BackgroundWorker = CType(sender, BackgroundWorker)
        BackgroundWorkers.Remove(bgw)
        If BackgroundWorkers.Count = 0 Then
            MessageBox.Show("Done!")
            Button1.Enabled = True
        End If
    End Sub

End Class

Open in new window

0
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 34183573
Hi StolenMan;

I have uploaded a sample project that you can download at this link.
https://filedb.experts-exchange.com/incoming/ee-stuff/8030-WindowsApplication3.zip 

The code looks like this:

Imports System.Threading

Public Class Form1

    ' A delegate to display info from a non UI thread
    Private Delegate Sub addToListBox_d(ByVal result As String)
    ' A count variable, one for each thread
    Private threadCounters As New List(Of Integer)()
    ' A count of the number of times to loop in the DoWork thread
    Private maxCount As Integer
    ' Used as a program lock to block other threads from using the
    ' code until the current thread is finish using it.
    Private threadLock As New Object

    ' To display results in the list box from another thread
    Private Sub addToListBox(ByVal result As String)
        If Me.InvokeRequired() Then
            Me.Invoke(New addToListBox_d(AddressOf addToListBox), New Object() {result})
        Else
            ListBox1.Items.Add(result)
        End If
    End Sub

    ' The thred code
    Private Sub DoWork(ByVal state As Object)
        ' Lock the code from beging used by other threads
        Monitor.Enter(threadLock)
        Try
            ' I assigned the thread number in the button click event to the Name
            ' property of the thread to identify the current executing thread which
            ' used to access the threadCounters List
            Dim threadIndex As Integer = Integer.Parse(Thread.CurrentThread.Name)
            Dim count As Integer = threadCounters(threadIndex)
            ' maxCount was identifed in the UI in the text box txtMaxCount
            For loopCount As Integer = 0 To maxCount - 1
                ' Update the UI with the thread name and loop index count
                addToListBox(Thread.CurrentThread.Name + " : " + loopCount.ToString())
            Next
            addToListBox("==========")
        Finally
            ' Before leaving the code release it for the next thread to use
            Monitor.Exit(threadLock)
        End Try
    End Sub

    Private Sub StartButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartButton.Click

        Dim counter As Integer = 0
        ' The comboBox cboNoOfThreads has the number of threads to create
        Dim threadCount As Integer = Integer.Parse(cboNoOfThreads.Text)

        If Integer.TryParse(txtMaxCount.Text, maxCount) Then
            Do While (counter < threadCount)
                Dim t As Thread = New Thread(AddressOf DoWork)
                t.IsBackground = True
                ' Name the thread so we can identify it in the thread code.
                t.Name = counter.ToString()
                ' Initialize the counter for this thread
                threadCounters.Add(0)
                t.Start(counter)
                counter += 1
            Loop
        End If

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'initialize to combo box to the first value in its list
        cboNoOfThreads.SelectedIndex = 0
    End Sub

End Class

Open in new window

0
 

Author Closing Comment

by:StolenMan
ID: 34183920
Thank you very much!
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 34184231
Not a problem; glad to help.  ;=)
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

604 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