954,557 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

System.ArgumentNullException in waithandle

hi,
I wanted to get the directories in a specific path
and I wrote that by threading programming
but when I call

waithandle.waitall(mywaithandles)


, I get the
unhandled exception of type 'System.ArgumentNullException' occurred in mscorlib.dll
Additional information: Value cannot be null.
error.

Can anyone help me what to do ?
there is my code of this part:

<MTAThread()> Public Sub file_search()
       
            Dim DD() As String = Directory.GetDirectories(Searcher_path)
            Dim num_of_dirs As Integer = get_unemptyfolders_num(DD)
            Dim mywaithandles(num_of_dirs) As AutoResetEvent
               For i As Integer = 0 To DD.Length - 1
                If is_emty(DD(i))=false Then
                    num_of_dirs -= 1
                    mywaithandles(num_of_dirs) = New AutoResetEvent(False)
                    Dim th_info As Threadinfo = New Threadinfo(DD(i), mywaithandles(num_of_dirs))
                    ThreadPool.QueueUserWorkItem(AddressOf searcher_thread, th_info)
                End If
            Next
            WaitHandle.WaitAll(mywaithandles)
end sub
loving3232
Light Poster
25 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

According to your code, the mywaithandles(0) may have no handle info at all.

I would suggest to sligly change your code to this untested example:

<MTAThread()> Public Sub file_search()
    Dim DD() As String = Directory.GetDirectories(Searcher_path)
    Dim mywaithandles() As AutoResetEvent = Nothing
    Dim currentHandleIdx as Integer = -1
    For i As Integer = 0 To DD.Length - 1
        If is_emty(DD(i))=false Then
            If mywaithandles Is Nothing Then
                ReDim mywaithandles(0)
            Else
                ReDim Preserve mywaithandles(mywaithandles.Length)
            End If
            currentHandleIdx = mywaithandles.Length -1
            mywaithandles(currentHandleIdx) = New AutoResetEvent(False)
            Dim th_info As Threadinfo = New Threadinfo(DD(i), mywaithandles(currentHandleIdx))
            ThreadPool.QueueUserWorkItem(AddressOf searcher_thread, th_info)
        End If
    Next
    If Not mywaithandles Is Nothing then WaitHandle.WaitAll(mywaithandles)
end sub


Hope this helps

lolafuertes
Master Poster
798 posts since Oct 2008
Reputation Points: 120
Solved Threads: 167
 

thanks for your reply lola
but I got my mistake that was too obvious
I had searched all folders,even ones that hadn't permission to access
since that was in a try, I couldn't find it
my waithandles became null in that way

anyway thanks

loving3232
Light Poster
25 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: