Visual Basic.NET - Problema con manualresetevent

   
Vista:

Problema con manualresetevent

Publicado por javier (13 intervenciones) el 08/03/2011 15:04:41
Hola,

Estoy realizando un ejemplo que consiste en el calculo de Fibonacci mediante multihilos, utilizando manualresetevent.

El problema que tengo, es que cuando ya ha ejecutado todos los hilos, me salta un error "argumentnullexception was unhandled" en la línea del "WaitHandle.WaitAll(doneEvents)", y no se porqué.

Este es el código, es el mismo que uno de c# que tengo, y que funciona, pero pasado a vb .net.

Muchas Gracias.

Un saludo.

Imports System.Threading

Module Module1

Sub Main()
Const FibonacciCalculations As Integer = 10

Dim doneEvents(FibonacciCalculations) As ManualResetEvent
Dim fibArray(FibonacciCalculations) As Fibonacci
Dim r As New Random

Console.WriteLine("launching " & FibonacciCalculations & " tasks...")
For i As Integer = 0 To FibonacciCalculations - 1
doneEvents(i) = New ManualResetEvent(False)
Dim f As Fibonacci = New Fibonacci(r.Next(10, 20), doneEvents(i))
fibArray(i) = f
System.Threading.ThreadPool.QueueUserWorkItem(AddressOf f.ThreadPoolCallBack, i)
Next

WaitHandle.WaitAll(doneEvents)
WaitHandle.WaitAny(doneEvents)
Console.WriteLine("Calculations complete.")

For j As Integer = 0 To FibonacciCalculations - 1
Dim f As Fibonacci = fibArray(j)
Console.WriteLine("Fibonacci(" & f.n & ") = " & f.fiboFN)
Next
Console.ReadLine()
End Sub

Class Fibonacci

Private _n As Integer
Public ReadOnly Property n() As String
Get
Return _n
End Get
End Property

Private _fiboFN As String
Public ReadOnly Property fiboFN() As String
Get
Return _fiboFN
End Get
End Property

Private _doneEvent As ManualResetEvent

Public Sub New(ByVal num As Integer, ByVal doneEvent As ManualResetEvent)
_n = num
_doneEvent = doneEvent
End Sub

Public Sub ThreadPoolCallBack(ByVal threadContext As Object)
Dim threadIndex As Integer = CInt(threadContext)
Console.WriteLine("thread " & threadIndex & " started ...")
_fiboFN = Calculate(_n)
Console.WriteLine("thread " & threadIndex & " result calculated ...")
_doneEvent.Set()
End Sub

Public Function Calculate(ByVal num As Integer) As Integer
If (num <= 1) Then
Return num
Else
Return Calculate(num - 1) + Calculate(num - 2)
End If
End Function
End Class

End Module
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder