|
-
Apr 16th, 2011, 01:11 PM
#1
Thread Starter
Member
[RESOLVED] Program Keeps Hanging
Hey, everyone! I made a program that backs up data from the My Documents, Favorites, and Desktop folders on Windows XP to a server. When the data is brought down from the server, it brings it down in Windows 7 format. So, for example, the directory to My Documents in Windows XP is "C:\Documents and Settings\username\My Documents." The same directory in Windows 7 is "C:\Users\username\Documents."
Everything works fine in terms of putting files and folders in the correct directory, but when bringing the data down from the server, the program sometimes freezes up and doesn't ever seem to get out of that state. It also makes the computer very slow and unresponsive after a while. Does anyone have any idea why this would occur?
Below, I have a code snippet of my copying code. I include a sleep command and a DoEvents command to halt the thread for a very short duration of time in order to make the UI more responsive.
Code:
Private Sub CopyAllFiles(ByVal Source As String, ByVal Destination As String)
Dim Text As String = vbNullString, FinalPath As String
Dim Dir As DirectoryInfo = New DirectoryInfo(Source), DestDir As DirectoryInfo = New DirectoryInfo(Destination)
Dim DirectoryList() As DirectoryInfo
Dim FileList() As FileInfo
If Not DestDir.Exists() Then
DestDir.Create()
End If
FileList = Dir.GetFiles()
For Each f As FileInfo In FileList
FinalPath = Path.Combine(Destination, f.Name)
' Code that prompts the user to replace the file if it exists already
' If you allow it to replace the file, or if the file doesn't exist, do the following
If CanReplace Then
DisplayText = DisplayText & vbNewLine & "Copying to " & FinalPath & "..."
Call UpdateText()
Try
f.CopyTo(FinalPath, True)
Catch
DisplayText = DisplayText & vbNewLine & "Error copying " & FinalPath & "..."
Call UpdateText()
End Try
System.Threading.Thread.Sleep(100)
Application.DoEvents()
End If
Next
DirectoryList = Dir.GetDirectories()
For Each d As DirectoryInfo In DirectoryList
FinalPath = Path.Combine(Destination, d.Name)
Call CopyAllFiles(d.FullName, FinalPath)
Next
End Sub
The UpdateText Sub is as follows:
Code:
Private Sub UpdateText()
frmDisplay.txtDisplay.Text = DisplayText
frmDisplay.txtDisplay.SelectionStart = frmDisplay.txtDisplay.Text.Length
frmDisplay.txtDisplay.ScrollToCaret()
End Sub
As you can see, it loops a lot, but it works pretty well sometimes. I believe I may have to use some multi-threading to get this to work properly, but I've looked it up and cannot find out how to implement it in my program. Any help would be greatly appreciated!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|