2013-10-09 3 views

Répondre

0

Je pense que je pourrais avoir résolu ..

Imports System.Threading 

Module Module1 

    Sub Main() 
     Dim t As New Tool 
     t.WaitToJoin() 
     Console.WriteLine("Done") 
     t.WaitToJoin() 
     Console.WriteLine("Done") 
     t.WaitToJoin() 
     Console.WriteLine("Done") 
     t.WaitToJoin() 
     Console.WriteLine("Done") 
     t.Update() 
     t.WaitToJoin() 
     Console.WriteLine("Done") 

     t.Update() 
     Thread.Sleep(1500) 
     t.Update() 
     t.WaitToJoin() 
     Console.WriteLine("Done") 

     t.Update() 
     Thread.Sleep(1500) 
     t.Update() 
     Thread.Sleep(1500) 
     t.Update() 
     Thread.Sleep(5500) 
     t.Update() 
     Thread.Sleep(10000) 
     t.Update() 
     t.WaitToJoin() 
     Console.WriteLine("Done") 
    End Sub 

End Module 

Public Class Tool 
    'Thread Stuff 
    Private threads As Integer = 0 
    Private threadsdone As AutoResetEvent = New AutoResetEvent(False) 

    Public Sub WaitToJoin() 
     If Interlocked.Read(threads) > 0 Then 
      threadsdone.WaitOne(New TimeSpan(0, 0, 30)) ' just to be sure not to lock forever, by some wierd reason, a timeout on 30 sec is added 
     End If 
    End Sub 

    Public Sub Update() 
     Threading.ThreadPool.QueueUserWorkItem(New Threading.WaitCallback(AddressOf HardWork), "Doing dome hard work...") 
    End Sub 

    Public Sub HardWork(ByVal state As Object) 
     Dim num As Integer = Interlocked.Increment(threads) 
     Console.WriteLine(num & " - " & state) 

     Thread.Sleep(10000) 
     If Interlocked.Decrement(threads) = 0 Then 
      threadsdone.Set() 
     End If 
    End Sub 
End Class 
1

Je ne pense pas que vous pouvez faire attendre Threadpool. Que diriez-vous d'utiliser un Task au lieu de threadpool? Quelque chose à l'effet comme le code suivant. Vous pouvez vérifier l'exemple complet here pour une meilleure compréhension

Dim taskA = Task.Factory.StartNew(Sub() DoSomeWork(10000000)) 
    taskA.Wait() 
    Console.WriteLine("taskA has completed.") 
+0

problème principal est bien que je dois savoir quand tous mes tâches sont faites, comme 1 la tâche pourrait durer plus longtemps que l'autre .. si je ne fais que suivre la dernière tâche, je pourrais superviser l'un de celui qui prend plus de temps – Droa

Questions connexes