Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7585977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:18:33+00:00 2026-05-30T19:18:33+00:00

I have the following script which creates 2 threads as you can see.The thing

  • 0

I have the following script which creates 2 threads as you can see.The thing is I would like this code to be tidy-er and the thread making better (such as a loop) but I haven’t been able to manage to create it.

Now i dim thread1 and thread 2 and have separate functions for both.
I am really new to vb.net so please dont shoot be down.

How would I make a public MaxThreads as integer = 2 (or whatever)
and then be able to
for i as 1 to MaxThreads
‘ what now is in function startthread1
next

Public Class UnCheckedItemReader
    Public ForceStop As Boolean = False ' to force shutdown in case the application is Closed
    Public UnCheckedItems As Integer ' to check and report
    Public QueueList As New Dictionary(Of Integer, Integer) ' this list will contain the addressbook ID and the number of the thread holding it
    Public QueueProcessed As New Dictionary(Of Integer, Integer) ' this will aloow us to update the Clientlistview

    Private ds As New DataSet
    Private da As SqlCeDataAdapter = New SqlCeDataAdapter()
    Private connection As New ConnectionClass
    Private table As String = "uncheckItems"

    'threads section - we have 2 threads
    Dim Thread1 As Threading.Thread
    Dim Thread2 As Threading.Thread

    Public Sub Controller()

        Do While ForceStop = False
            If QueueList.Count < 1 Then
                GetDatabaseSqlClientListUnCheckedItems()
                PopulateQueueList()
                StartThreads()
            End If
            Threading.Thread.Sleep(5000)
            Console.WriteLine("New Round")
        Loop


    End Sub



    Private Sub StartThreads()
        StartThread1()
        StartThread2()
    End Sub

    Private Sub StartThread1()
        Dim DS As New RelianProcess
        DS.id = 5
        DS.ThreadId = 1
        DS.QueueList = QueueList

        AddHandler DS.ShowError, AddressOf ShowErrorThread1
        AddHandler DS.Stopit, AddressOf StopThread1
        Thread1 = New System.Threading.Thread(AddressOf DS.StartTimer)
        Thread1.IsBackground = True

        Try
            Thread1.Start()
        Catch e As ThreadStateException
            Console.WriteLine("Caught: {0}", e.Message)
        Catch ex As Exception
            Console.WriteLine("Caught: {0}", ex.Message)
        End Try
    End Sub

    Private Sub StartThread2()
        Dim DS As New RelianProcess
        DS.id = 5
        DS.ThreadId = 2
        DS.QueueList = QueueList

        AddHandler DS.ShowError, AddressOf ShowErrorThread1
        AddHandler DS.Stopit, AddressOf StopThread1
        Thread2 = New System.Threading.Thread(AddressOf DS.StartTimer)
        Thread2.IsBackground = True

        Try
            Thread2.Start()
        Catch e As ThreadStateException
            Console.WriteLine("Caught: {0}", e.Message)
        Catch ex As Exception
            Console.WriteLine("Caught: {0}", ex.Message)
        End Try
    End Sub




    Private Sub ShowErrorThread1(ByVal e As Exception, ByVal id As Integer)
        Console.Write(e.ToString)
        QueueList.Remove(id)

        Console.Write(vbCrLf + "Removed from Mother queuelist " + CStr(id))
        QueueProcessed.Add(id, CInt(GetCurrentUnixTimestamp()))
        Console.Write(vbCrLf + "Added to processed list " + CStr(id))
    End Sub

    Private Sub ShowErrorThread2(ByVal e As Exception, ByVal id As Integer)
        Console.Write(e.ToString)
        QueueList.Remove(id)

        Console.Write(vbCrLf + "Removed from Mother queuelist " + CStr(id))
        QueueProcessed.Add(id, CInt(GetCurrentUnixTimestamp()))
        Console.Write(vbCrLf + "Added to processed list " + CStr(id))
    End Sub

    Private Sub StopThread1()
        Thread1.Abort()
    End Sub

    Private Sub StopThread2()
        Thread2.Abort()
    End Sub


    Private Sub PopulateQueueList()
        Dim i As Integer = 1
        If ds.Tables(table).Rows.Count > 0 Then
            For Each irow As DataRow In ds.Tables(table).Rows
                Try
                    If QueueList.ContainsKey(CInt(irow(0))) = False Then
                        i = i + 1
                        If i = 3 Then 
                            i = 1
                        End If

                        QueueList.Add(CInt(irow(0)), i)
                        Console.WriteLine(ControlChars.NewLine + "Added item {0} to thread {1}", irow(0), i)

                    End If
                Catch e As Exception
                    Console.WriteLine(ControlChars.NewLine + "Exception Raised. The following error occured : {0}", e.Message)
                End Try
            Next
        End If
    End Sub
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T19:18:34+00:00Added an answer on May 30, 2026 at 7:18 pm

    Instead of declaring each thread individually, use a Collection of Threads. Something along these lines:

        Dim m_threads As New List(Of Threading.Thread)
    Dim m_maxThreads As Int32 = 5
    
    Private Sub StartAllThreads()
    
    
        For pos As Int32 = 1 To m_maxThreads
            StartThread(pos)
        Next
    End Sub
    
    Private Sub StartThread(p_threadID As Int32)
        Dim tempThread As Threading.Thread
        Dim DS As New RelianProcess
    
    
        DS.id = 5
        DS.ThreadId = p_threadID
        DS.QueueList = QueueList
    
        tempThread = New System.Threading.Thread(AddressOf DS.StartTimer)
        m_threads.Add(tempThread)
    
        AddHandler DS.ShowError, AddressOf ShowErrorThread1
        AddHandler DS.Stopit, AddressOf StopThread1
        tempThread.IsBackground = True
    
        Try
            tempThread.Start()
        Catch e As ThreadStateException
            Console.WriteLine("Caught: {0}", e.Message)
        Catch ex As Exception
            Console.WriteLine("Caught: {0}", ex.Message)
        End Try
    
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following script which creates an email from a contact form: http://codeviewer.org/view/code:1b54
I have the following code which creates a map on a content page. <script
I have the following Ruby script that creates a Debian package, which works fine:
I would like to create a Makefile which also creates a simple script for
I have the following code which has been passed on to me and creates
I have the following script which first shows only the first para of the
I have the following script which appears multiple times on my page. I have
I have the following script, which is working for the most part Link to
I have a problem with following script. It generates a list of places which
I have an image uploading script in which i use the following setup to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.