Click here to Skip to main content
15,894,017 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Can someone please tell me if I this coded correctly. I eventually get an error that cannot access disposed object on finished.signal in the finally loop

VB
Dim scanAutoResetEvent As New AutoResetEvent(True)
    Private Sub ScanDirectory(dir As String)
        Try
            scanAutoResetEvent.WaitOne()
            If _config.LogLevel.ToLower = "high" Then
                LogMessages("Scanning: " + dir, Reflection.MethodBase.GetCurrentMethod.Name)
            End If

            Dim _di As DirectoryInfo = New DirectoryInfo(dir)
            Using finished As New CountdownEvent(1)
                For Each _xmlFile In _di.EnumerateFiles("*.xml", SearchOption.AllDirectories)
                    Dim _ri As ReportInfo = New ReportInfo()
                    _ri.XMLINfo = _xmlFile
                    _ri.Pad = _xmlFile.Directory.Parent.ToString

                    If _config.Threading Then
                        ThreadPool.QueueUserWorkItem(New WaitCallback(Function(x)
                                                                          Try
                                                                              LoadToDepcon(_ri)
                                                                          Finally
                                                                              finished.Signal()
                                                                          End Try
                                                                      End Function))
                    Else
                        LoadToDepcon(_ri)
                    End If
                    'End If
                Next
                finished.Signal()
                finished.Wait()
            End Using
        Catch ex As Exception
            LogMessages(ex.Message, Reflection.MethodBase.GetCurrentMethod.Name, 300, "E", False, True)
        Finally
            scanAutoResetEvent.Set()
        End Try
    End Sub
Posted
Comments
Sergey Alexandrovich Kryukov 18-Sep-14 17:03pm    
Please, in what line? Please understand that "if I this coded correctly" depends on what did you try to achieve. Why not explaining a bit more?
What is "finished"? Where is its declaration? What is the idea of using it? You share one instance between thread and call Signal on each, why?
—SA
Member 10198178 18-Sep-14 17:42pm    
You are correct.

There is threading above this with a timer. I want to prevent this method to be executing more than once hence the manualresetevent. However I'm threading within the method to call another method with the ThreadPoolQueueWorkUser. I want to make sure all those threads are completed before ending this method.
Olivier Levrey 23-Sep-14 8:32am    
SA didn't receive your message because you should use the "Reply" button next to his name. Reply to him and he will get back to you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900