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 は、コード行 num に対応する構文をサポートしていません。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 を使用した Thread コンストラクターの呼び出しに関するこの MSDN の記事を参照してください。あなたはVBにいるので、これを行うことができるはずです:

Dim t As New Thread(AddressOf CheckPathFunction)

t.Start(path)

さて、それがスレッドを起動するための答えです。しかし、スレッドが完了したかタイムアウトした場合にのみ、ファイルが存在するかどうかを実際に返すのではなく、SteveDog に同意します。

戻り値への編集: その値を戻す 1 つの方法は、パスだけでなく、オブジェクトを渡すことです。次に、オブジェクトを使用して結果を返します。

したがって、次のようにクラスを宣言します。

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 に答える