2012-08-29 136 views
0

我在VB .NET下面的代碼示例2008等效代碼2005

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框架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 

謝謝

+1

在原始代碼中,它會從「CheckPathFunction」方法獲取返回值嗎?只要方法在指定的超時時間內完成,它就會返回true,但它實際上並不會查看該方法是否找到該文件。 –

+0

@SteveDog,如果在未達到超時時檢測到文件存在或沒有達到,則返回相應的值,如果在達到超時時無法確定文件是否存在返回false,則此函數用於檢查文件是否存在在網絡共享中 –

回答

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 
2

請參閱this MSDN article使用ParameterizedThreadSta調用Thread構造函數rt代表。既然你在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 
+0

如果在超時發生之前檢測到文件存在或不存在,它會返回適當的值True或False,否則如果存在的檢查比超時更多的時間,那麼也返回False –

+1

嘗試運行一些測試,找不到文件。我對它進行了現狀測試,當它們應該是錯誤的時候它們又回來了。對Join的調用不會返回CheckPathFunction的返回值,它只會返回是否已完成Thread或超時。 – eol

+0

您的示例在VB .net 2005中是否真正適用於您?我在代碼行中獲取錯誤參數錯誤消息創建新線程「dim t as new Thread(AddressOf ChecPAthFunction)」 –