[Help]How to wait for a function to finish?

Posts 1–10 of 10 · Page 1 of 1
[Help]How to wait for a function to finish?
I'm currently using a CopyFiles function to copy files from different places but the problem is, when I use the function, they all 'pile' on top of each other and I want it so it waits for 1 to finish then do the next and etc...
[php]
Public Sub CopyFiles(ByVal sourcePath As String, ByVal DestinationPath As String)
Dim start As Integer
Dim prog As Decimal
If (Directory.Exists(sourcePath)) Then
ProgressBar1.Value = 0
start = 0
prog = System.IO.Directory.GetFiles(sourcePath).Length
For Each fName As String In Directory.GetFiles(sourcePath)
If File.Exists(fName) Then
Dim dFile As String = String.Empty
dFile = Path.GetFileName(fName)
Dim dFilePath As String = String.Empty
dFilePath = DestinationPath + dFile
File.Copy(fName, dFilePath, True)
start += 1
ProgressBar1.Value = (start * 100) / prog
End If
Next
End If[/php]

If there is a better way to copy files, please elaborate.
I don't understand what you mean by "they all 'pile' on top of each other"... Care to elaborate?
Well by nature the next line of code won't be executed until the conclusion of the previous line (in the same thread), I think your problem lies in your actual code not copying at all, here's my "revised" look at your code.

[php]
Public Sub CopyFiles(ByVal sourcePath As String, ByVal DestinationPath As String)

If (Directory.Exists(sourcePath)) Then
ProgressBar1.Value = 0
Dim allFiles() As String = Directory.GetFiles(sourcePath)
ProgressBar1.Maximum = allFiles.Length

For Each filePath As String In allFiles
If File.Exists(filePath) Then
Dim dFile As String = Path.GetFileName(filePath)
Dim dFilePath As String = Path.Combine(DestinationPath, dFile)
File.Copy(filePath, dFilePath, True)
ProgressBar1.Value += 1
End If
Next
End If

MessageBox.Show("Files copied", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Information)
ProgressBar1.Value = 0
End Sub
[/php]

I think that should do the trick, but I could be wrong as I didn't test it.
I suggest using a multithread, too.

Just drop me a message on MSN if you need help.
Meh, please mark as solved. Solved it myself /
Quote Originally Posted by master131 View Post
Meh, please mark as solved. Solved it myself /
Would you care to share your solution? Also, did you test if my code worked? I'm just interested to see if what I thought was the problem actually was.
Err, I did something that has absolutely nothing to do with this. /
Edited title to use [Help] Tags

Remember to tag your title, it will help us know what it is you are looking for.
[Help] , [Release] , [Opinion], Etc , Etc

Thank You
Funny he said mark it as solved /

Cod:Mw2 thing
Quote Originally Posted by Insane View Post
Funny he said mark it as solved /

Cod:Mw2 thing
Eh, I was half asleep /
Posts 1–10 of 10 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Need help?