Search code examples
.netvb.netequivalent

Equivalent code for VB .net 2005


I have the following code sample in VB .net 2008

Public Function CheckPathFunction(ByVal path As String) As Boolean
    Return System.IO.File.Exists(path)
End Function

Public Function PathExists(ByVal path As String, ByVal timeout As Integer) As Boolean
    Dim exists As Boolean = True
    Dim t As New Thread(DirectCast(Function() CheckPathFunction(path), ThreadStart))

    t.Start()

    Dim completed As Boolean = t.Join(timeout)
    If Not completed Then
        exists = False
        t.Abort()
    End If

    Return exists
End Function

Unfortunately I have to work with Vb .net 2005 and net framework 2.0; How can I accomplish the same for VB .net 2005?, VB .net 2005 does not support the syntax corresponding to the code line num. 3:

Function() CheckPathFunction(path)

Please note that the function to call requires a parameter and returns a value


I've tried using a delegate as indicated next but does not work

Private Delegate Function CheckPath(ByVal path As String) As Boolean

Public Function CheckPathFunction(ByVal path As String) As Boolean
    Return IO.File.Exists(path)
End Function

Public Function PathExists(ByVal path As String, ByVal timeout As Integer) As Boolean
    Dim checkPathDelegate As New CheckPath(AddressOf CheckPathFunction)

    Dim exists As Boolean = True
    Dim t As New Thread(checkPathDelegate(path))

    t.Start()

    Dim completed As Boolean = t.Join(timeout)
    If Not completed Then
        exists = False
        t.Abort()
    End If

    Return exists
End Function

Thanks


Solution

  • Taking as base structure the code by @eol the final working code is:

     Class KeyValuePair
          Public Path As String
          Public Found As Boolean
     End Class
    
     Public Sub CheckPathFunction(ByVal dataObject As Object)
          dataObject.Found = IO.Directory.Exists(dataObject.Path)
     End Sub
    
     Public Function PathExists(ByVal path As String, ByVal timeout As Integer) As Boolean
          Dim exists As Boolean
    
          Dim data As New KeyValuePair
          data.Path = path
    
          Dim t As New Thread(New ParameterizedThreadStart(AddressOf CheckPathFunction))
          t.Start(data)
    
          Dim completed As Boolean = t.Join(timeout)
          If Not completed Then
               exists = False
               t.Abort()
          Else
               exists = data.Found
          End If
    
          Return exists
     End Function