0

我在 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

不幸的是,我必须使用 Vb .net 2005 和 net framework 2.0;我怎样才能为 VB .net 2005 完成相同的操作?,VB .net 2005 不支持与代码行编号对应的语法。3:

Function() CheckPathFunction(path)

请注意,要调用的函数需要一个参数并返回一个值


我已尝试使用如下所示的委托,但不起作用

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

谢谢

4

2 回答 2

2

请参阅有关使用 ParameterizedThreadStart Delegate 调用线程构造函数的MSDN 文章。既然你在 VB 中,你应该能够做到这一点:

Dim t As New Thread(AddressOf CheckPathFunction)

t.Start(path)

好吧,这就是启动线程的答案。但我同意 SteveDog,它实际上并没有返回文件是否存在,只有当线程完成或超时时。

编辑返回值:获取该值的一种方法是传入一个对象,而不仅仅是路径。然后使用对象传回结果。

所以声明一个类,比如:

Class DataHolder
    Public Path As String
    Public Found As Boolean
End Class

将 CheckPathFunction 更改为:

Public Sub CheckPathFunction(ByVal rawData As Object)
    Dim data As DataHolder = DirectCast(rawData, DataHolder)
    data.Found = System.IO.File.Exists(data.Path)
End Sub

并将 PathExists 更改为:

Public Function PathExists(ByVal path As String, ByVal timeout As Integer) As Boolean
    Dim exists As Boolean
    Dim t As New Thread(AddressOf CheckPathFunction) '  (DirectCast(Function() CheckPathFunction(path), ThreadStart))
    Dim data As DataHolder = New DataHolder
    data.Path = path
    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
于 2012-08-29T17:23:44.873 回答
1

以@eol的代码为基础结构,最终的工作代码是:

 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
于 2012-08-29T20:46:43.370 回答