Simultaneous Threads

andyred

Active member
Joined
Mar 5, 2009
Messages
27
Programming Experience
1-3
Hi Guys. One slight problem here

OK - I have a class that wraps your worker thread and logic to pass over a list of arrays, e.g.

VB.NET:
Public Class ExtractWorker 

    ' Private 
    Private mUrls As List(Of String) 
    Private mWorkerId As String 
    Private mWorkerThread As Threading.Thread 

    Public Sub New(ByVal urlsToProcess As List(Of String), ByVal workerId As String) 
        ' Set properties 
        mUrls = urlsToProcess 
        mWorkerId = workerId 
    End Sub 

    Public Sub StartProcessing() 
        ' Creates a new thread to start the internal processing 
        mWorkerThread = New Threading.Thread(AddressOf ProcessArray) 
        mWorkerThread.Name = "Worker " & mWorkerId 
        mWorkerThread.Start() 
    End Sub 

    Private Sub ProcessArray() 
        ' Runs through the array processing each Url 
        For Each url As String In mUrls 
            ' Output 
            Debug.WriteLine("Worker: " & mWorkerId & " processing url: " & url) 
            ' Do your extraction code here 
            ' Use thread.sleep to emulate processing 
            Threading.Thread.Sleep(5) 
        Next 
    End Sub 

End Class

Then to start the workers, I have a list of dummy URLs and create the worker instances, e.g

VB.NET:
  Dim testUrls As New List(Of String) 
        For n As Integer = 1 To 100 
            testUrls.Add("www.mysite.com/" & n.ToString) 
        Next 

 Dim threadsNo as Integer=15 
 Dim workers As New List(Of ExtractWorker) 
        For n As Integer = 0 To threadsNo 
            Dim ew As New ExtractWorker(testUrls.GetRange(n, testUrls.count / threadsNo), n.ToString()) 
            workers.Add(ew) 
            ew.StartProcessing() 
Next


My problem is: This code creates 15 threads (one thread-one url) taking care of first 15 urls in testUrls. How do I make these threads start again from url 16 and so on until they finish the entire testUrls list (100 items or urls in this case)?

Thanks
 
Here's the code, exactly as you mentioned it and it's not stopping:

VB.NET:
Imports System.Threading

Public Class Form1
    Private canceled As Boolean
    Public Sub DoWork()
        Dim isDone As New AutoResetEvent(False)
        canceled = False
        ThreadPool.SetMaxThreads(15, 15)
        ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf TaskA), isDone)
        Dim ThreadCount, ThreadPorts As Integer
        Dim workerThreads, completionPortThreads As Integer

        ThreadPool.GetAvailableThreads(ThreadCount, ThreadPorts)
        Console.WriteLine("Thread pool size {0}", ThreadCount)

        ThreadPool.GetMaxThreads(workerThreads, completionPortThreads)
        Console.WriteLine("workerThreads: {0} completionPortThreads: {1}", workerThreads, completionPortThreads)

        Console.WriteLine("Waiting for asynchronous threads to complete.")

    End Sub

    Public Sub TaskA(ByVal state As Object)
        Dim I As Integer
        If canceled = True Then Return
        For I = 0 To 500000
            System.Console.Write("A")
        Next
        Thread.Sleep(3000)
        CType(state, AutoResetEvent).Set()
    End Sub

    Private Sub stopbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles stopbtn.Click
        canceled = True
    End Sub

    Private Sub startbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles startbtn.Click
        DoWork()
    End Sub
End Class

Can you please tell me why
 
How slow is your computer really, if you think you can click two buttons faster than the cpu can start your single worker thread and check the value of a variable? Think twice! Did you see any "A" letters in console output before you clicked the cancel button? If so, where in code do you think you worker method is, at what code line?
 
Yes I am seeing alot of A's in my console before clicking the cancel button. I see A's also after clicking it. Mycode is t he the line where is writing A's to my console.

Since I'm clicking the cancel button cancel=true and the loop should stop.

Or is something I am not understanding here?!

In this case I'm not seeing any possible way to stop the threadpool for my single worker
 
Or is something I am not understanding here?!
Absolutely. How can your code stop if it is not checking if it should stop? You are running this loop:
For I = 0 To 500000
System.Console.Write("A")
Next
Where in this loop do you check the cancel variable? Do you actually expect when you set cancel=True that the method running this loop will just stop and step back to a previous code line to check if the cancel variable now is True? That is not how computer programs work simply, they execute one line after the other, the only time a code line is executed repeatedly is during declared loops. I think you have a so limited understanding of the fundamentals of programming that you should read the beginner book all over again.
 
I'm very tired, I haven't slept for over 3 nights now. I was working 24/7. I think i will take a nap and continue after that. I'm making big mistakes and I'm wasting alot of your time.
John please let me thank you kindly for your help, you've been more than pacient and understanding with me.
I'll start fresh tomorrow.
I really appreciate your help and many thanks again

P.S. You're right my brain is really sleep limited now :)
 
Everything seems clear after a good night sleep.
Thanks for your patience John.

One question: Let's say I have a listbox of 100 urls like http://mysite.com/page1
http://mysite.com/page2
.................................
http://mysite.com/page100

The threads are jumping over several urls randomly. Have you seen this thing before?

For example it jumps from page2 to page4. The pages exist on the website.
 
Yes, it is normal and expected. Even if threads are dequeued from pool in the order they were added the order which they are executed and finish is not determined. A single line of code from a high level language as VB.Net may translate to several cpu instructions, which has its own queueing system.
If you need the tasks to run in order then you don't need multiple threads, one thread that calls the tasks in order or in a loop can do this.
 
Back
Top