C
Chet.Horton
Guest
I am trying to learn about running parallel task/async tasks, what the difference is, which is faster, and can you use both together to delete and copy files to and from a network share. This what I have done so far (based off few different examples I found) and it works pretty fast across a VPN connection and super fast when I test it on a folder locally.
Private Async Sub pbStart_Click(sender As Object, e As EventArgs) Handles pbStart.Click
Fold = tbFolder.Text
'This runs on the UI thread
Console.WriteLine("Starting...")
'This runs on a Thread Pool thread
Dim result As Integer = Await Task.Run(Function()
workResult =
EmptyFolder(Fold)
Return workResult
Console.WriteLine("Thread Finished")
End Function)
'This runs again on the UI thread
Console.WriteLine("UI Finished")
End Sub
Function EmptyFolder(ByVal pathName As String) As Boolean
Dim errors As Boolean = False
Dim dir As DirectoryInfo = New DirectoryInfo(pathName)
For Each fi As FileInfo In dir.EnumerateFiles()
Try
fi.IsReadOnly = False
fi.Delete()
While fi.Exists
System.Threading.Thread.Sleep(10)
fi.Refresh()
End While
Catch e As IOException
Debug.WriteLine(e.Message)
errors = True
End Try
Next
For Each di As DirectoryInfo In dir.EnumerateDirectories()
Try
EmptyFolder(di.FullName)
di.Delete()
While di.Exists
System.Threading.Thread.Sleep(10)
di.Refresh()
End While
Catch e As IOException
Debug.WriteLine(e.Message)
errors = True
End Try
Next
Return Not errors
End Function
How can I use parallel for each in my in the emptyfolder function or should I try parallel for instead? Does it even add any performance boost or does it cause more problems/bottlenecks other than the VPN connection?
Continue reading...
Private Async Sub pbStart_Click(sender As Object, e As EventArgs) Handles pbStart.Click
Fold = tbFolder.Text
'This runs on the UI thread
Console.WriteLine("Starting...")
'This runs on a Thread Pool thread
Dim result As Integer = Await Task.Run(Function()
workResult =
EmptyFolder(Fold)
Return workResult
Console.WriteLine("Thread Finished")
End Function)
'This runs again on the UI thread
Console.WriteLine("UI Finished")
End Sub
Function EmptyFolder(ByVal pathName As String) As Boolean
Dim errors As Boolean = False
Dim dir As DirectoryInfo = New DirectoryInfo(pathName)
For Each fi As FileInfo In dir.EnumerateFiles()
Try
fi.IsReadOnly = False
fi.Delete()
While fi.Exists
System.Threading.Thread.Sleep(10)
fi.Refresh()
End While
Catch e As IOException
Debug.WriteLine(e.Message)
errors = True
End Try
Next
For Each di As DirectoryInfo In dir.EnumerateDirectories()
Try
EmptyFolder(di.FullName)
di.Delete()
While di.Exists
System.Threading.Thread.Sleep(10)
di.Refresh()
End While
Catch e As IOException
Debug.WriteLine(e.Message)
errors = True
End Try
Next
Return Not errors
End Function
How can I use parallel for each in my in the emptyfolder function or should I try parallel for instead? Does it even add any performance boost or does it cause more problems/bottlenecks other than the VPN connection?
Continue reading...