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
 
You can use the counting method I described in post 7 (5), when total=completed (progress 100%) all is completed.
 
Managed to make the progress work. can you please show me how to use that cancel variable to abort the thread pool when clicking a button (in my case). I cannot make it work
 
VB.NET:
Sub worker()
  If cancel Then Return
  'do some work
End Sub
 
In my case this is TaskA sub right?
Where do I initialize cancel=false? at the beggining of TaskA sub?
How does the application link the cancel variable to the threadpool stop?
Sorry, I'm such a nuissance but I really want to learn how to do it properly
 
In my case this is TaskA sub right?
absolutely
Where do I initialize cancel=false? at the beggining of TaskA sub?
no, before you start the worker thread(s), else the worker would read cancel=True and cancel its work, right?
How does the application link the cancel variable to the threadpool stop?
wherever you want you set the cancel variable to True, any code that is performed after that can read that the cancel variable is True.
 
I have done it exactly as you have said. I have added to the stop button me.cancel=true.
When I press the button it continues to process items, the threadpool is not stopping.
Any ideas?
 
Threadpool will not stop, any queued method will execute, the point is the method should check and skip the work if cancel=true. Doing this the pool will clear in a split second. Currently executing methods will not stop unless they check cancel and skip rest of work, this is only possible during a loop or between other calls, a single long-running call is not possible stop.
 
If not already answered in this thread, what do you mean?
 
Do you need to add something else? I don't see why or what that should be.
 
Was it the code in post 18 you didn't understand?
 
I have used that exact code and the threads continue to process items.
Theoretically if i press a button which states that cancel=true the next threads should stop right?
 
Theoretically if i press a button which states that cancel=true the next threads should stop right?
Yes, any code that read the variable after it is changed will read the new value.
 
Back
Top