Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 4th, 2007, 3:30 PM   #1
PhilBon
Hobbyist Programmer
 
PhilBon's Avatar
 
Join Date: Nov 2005
Posts: 171
Rep Power: 3 PhilBon is on a distinguished road
Send a message via AIM to PhilBon Send a message via MSN to PhilBon
Preventing Timeout Message During Long Processes

So currently I am working on a program to backup files. What it does is search through each directory recursively like so:
vbnet Syntax (Toggle Plain Text)
  1. Public Sub BackUp(ByVal root As String)
  2. Dim FoundFile, FoundDirectory As String
  3. 'Loop thru each of the files in this directory
  4. For Each FoundFile In My.Computer.FileSystem.GetFiles(root)
  5. If (IO.File.GetAttributes(FoundFile) = IO.FileAttributes.Archive) Or RootCreated Then
  6. ListBox1.Items.Add(FoundFile)
  7. ListBox1.Update()
  8. Dim FileName As String
  9. FileName = RemoveSource(FoundFile, Source)
  10. Try
  11. IO.File.Copy(FoundFile, Destination & FileName)
  12. Catch ex As Exception
  13. Dim DestFile As String = Destination & FileName
  14. Dim DestFileArr() As String = DestFile.Split("\")
  15. Dim l As Integer
  16. Dim folder As String
  17. folder = DestFileArr(0)
  18. For l = 1 To DestFileArr.GetUpperBound(0) - 1
  19. folder = folder & "\" & DestFileArr(l)
  20. If Not IO.Directory.Exists(folder) Then
  21. IO.Directory.CreateDirectory(folder)
  22. End If
  23. Next
  24. Try
  25. IO.File.Copy(FoundFile, Destination & FileName)
  26. Catch ex1 As Exception
  27. MsgBox("Error:" & Chr(13) & ex1.Message & Chr(13) & " No Folder for the file to be placed in")
  28. End Try
  29. End Try
  30. ' IO.File.SetAttributes(FoundFile, IO.FileAttributes.Normal)
  31. IO.File.SetCreationTime(Destination & FileName, IO.File.GetCreationTime(FoundFile))
  32. IO.File.SetLastAccessTime(Destination & FileName, IO.File.GetLastAccessTime(FoundFile))
  33. IO.File.SetAccessControl(Destination & FileName, IO.File.GetAccessControl(FoundFile))
  34. IO.File.SetLastWriteTime(Destination & FileName, IO.File.GetLastWriteTime(FoundFile))
  35. End If
  36. Next
  37. 'Loop thru each directory in this directory
  38. For Each FoundDirectory In My.Computer.FileSystem.GetDirectories(root)
  39. 'Give the user feedback about progress
  40. 'LabelSearch.Text = "Scanning: " & FoundDirectory
  41. 'LabelSearch.Update()
  42.  
  43. 'Recursively call this very same sub procedure!
  44. BackUp(FoundDirectory)
  45. Next
  46. End Sub
I have a problem after I hit about the 100th file, it "dies" and the compiler is like it's taking to long. It then asks me if I want to continue and I say yes. How do I stop that? I've tried GC.KeepAlive(me), for each file but that didn't seem to work. This is going to be used for directories that could possibly have over 100k files and I don't want to have to sit and hit yes every so many times.
PhilBon is offline   Reply With Quote
Old Jul 5th, 2007, 1:01 PM   #2
melbolt
Hobbyist Programmer
 
melbolt's Avatar
 
Join Date: Feb 2005
Location: PA, USA
Posts: 229
Rep Power: 4 melbolt is on a distinguished road
Send a message via AIM to melbolt Send a message via Yahoo to melbolt
maybe try putting it in a new thread and see what happens
__________________
I have never let my schooling interfere with my education. -Mark Twain-

Xbox live gamertag: melbolt
melbolt is offline   Reply With Quote
Old Jul 5th, 2007, 1:30 PM   #3
PhilBon
Hobbyist Programmer
 
PhilBon's Avatar
 
Join Date: Nov 2005
Posts: 171
Rep Power: 3 PhilBon is on a distinguished road
Send a message via AIM to PhilBon Send a message via MSN to PhilBon
What do you mean? Like have something that starts a new thread for each sub folder? If so how would I go about doing that? and What if that folder has 1000 files in it too?
PhilBon is offline   Reply With Quote
Old Jul 5th, 2007, 2:12 PM   #4
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 730
Rep Power: 4 Dameon is on a distinguished road
Is this function running on the main thread (it is if you didn't create any) in a WinForms app?
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270
Dameon is offline   Reply With Quote
Old Jul 5th, 2007, 2:46 PM   #5
PhilBon
Hobbyist Programmer
 
PhilBon's Avatar
 
Join Date: Nov 2005
Posts: 171
Rep Power: 3 PhilBon is on a distinguished road
Send a message via AIM to PhilBon Send a message via MSN to PhilBon
it's a windows from, here is all the code:
vbnet Syntax (Toggle Plain Text)
  1. Public Class Form1
  2. Private Source, Destination As String
  3. Private RootCreated As Boolean
  4. Private Sub BTNSelSour_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNSelSour.Click
  5. FBD.ShowDialog()
  6. If Not (FBD.SelectedPath = Nothing) Then
  7. Source = FBD.SelectedPath
  8. TXTSour.Text = Source
  9. End If
  10. FBD.SelectedPath = Nothing
  11.  
  12. End Sub
  13.  
  14. Private Sub BTNSelDest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNSelDest.Click
  15. FBD.ShowDialog()
  16. If Not (FBD.SelectedPath = Nothing) Then
  17. Destination = FBD.SelectedPath
  18. TXTDest.Text = Destination
  19. End If
  20. FBD.SelectedPath = Nothing
  21. End Sub
  22.  
  23. Private Sub BTNBackUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNBackUp.Click
  24. If Source = Nothing Then
  25. MsgBox("The Source is missing")
  26. Exit Sub
  27. End If
  28. If Destination = Nothing Then
  29. MsgBox("The Destination is missing")
  30. Exit Sub
  31. End If
  32. If IO.Directory.Exists(Destination & "\root") Then
  33. Dim CurrentTime As DateTime = Now()
  34. Dim CurrentBackUp As String = CurrentTime
  35. CurrentBackUp = CurrentBackUp.Replace(" ", "_")
  36. CurrentBackUp = CurrentBackUp.Replace("/", "-")
  37. CurrentBackUp = CurrentBackUp.Replace(":", "--")
  38. Destination = Destination & "\" & CurrentBackUp
  39. Else
  40. Destination = Destination & "\root"
  41. RootCreated = True
  42. End If
  43. IO.Directory.CreateDirectory(Destination)
  44. BackUp(Source)
  45. End Sub
  46. Public Sub BackUp(ByVal root As String)
  47. Dim FoundFile, FoundDirectory As String
  48. 'Loop thru each of the files in this directory
  49. Try
  50. For Each FoundFile In My.Computer.FileSystem.GetFiles(root)
  51.  
  52.  
  53. If (IO.File.GetAttributes(FoundFile) = IO.FileAttributes.Archive) Or RootCreated Then
  54. 'ListBox1.Items.Add(FoundFile)
  55. 'ListBox1.Update()
  56. Dim FileName As String
  57. FileName = RemoveSource(FoundFile, Source)
  58. Try
  59. IO.File.Copy(FoundFile, Destination & FileName)
  60. Catch ex As Exception
  61. Dim DestFile As String = Destination & FileName
  62. Dim DestFileArr() As String = DestFile.Split("\")
  63. Dim l As Integer
  64. Dim folder As String
  65. folder = DestFileArr(0)
  66. For l = 1 To DestFileArr.GetUpperBound(0) - 1
  67. folder = folder & "\" & DestFileArr(l)
  68. If Not IO.Directory.Exists(folder) Then
  69. IO.Directory.CreateDirectory(folder)
  70. End If
  71. Next
  72. Try
  73. IO.File.Copy(FoundFile, Destination & FileName)
  74. Catch ex1 As Exception
  75. MsgBox("Error:" & Chr(13) & ex1.Message & Chr(13) & " No Folder for the file to be placed in")
  76. End Try
  77. End Try
  78. Try
  79. ' IO.File.SetAttributes(FoundFile, IO.FileAttributes.Normal)
  80. IO.File.SetCreationTime(Destination & FileName, IO.File.GetCreationTime(FoundFile))
  81. IO.File.SetLastAccessTime(Destination & FileName, IO.File.GetLastAccessTime(FoundFile))
  82. IO.File.SetAccessControl(Destination & FileName, IO.File.GetAccessControl(FoundFile))
  83. IO.File.SetLastWriteTime(Destination & FileName, IO.File.GetLastWriteTime(FoundFile))
  84. Catch
  85.  
  86. End Try
  87. End If
  88. Next
  89. Catch
  90. End Try
  91. Try
  92. 'Loop thru each directory in this directory
  93. For Each FoundDirectory In My.Computer.FileSystem.GetDirectories(root)
  94. 'Give the user feedback about progress
  95. 'LabelSearch.Text = "Scanning: " & FoundDirectory
  96. 'LabelSearch.Update()
  97.  
  98. 'Recursively call this very same sub procedure!
  99. BackUp(FoundDirectory)
  100. Next
  101. Catch
  102. End Try
  103. End Sub
  104. Function RemoveSource(ByVal Input As String, ByVal Source As String) As String
  105. Dim SourceRootLen As Integer
  106. SourceRootLen = Source.Length
  107. Return Input.Substring(SourceRootLen)
  108. End Function
  109.  
  110. Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  111. RootCreated = False
  112. End Sub
  113. End Class
Attached Images
File Type: jpg BU4.jpg (13.0 KB, 22 views)
PhilBon is offline   Reply With Quote
Old Jul 5th, 2007, 6:20 PM   #6
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 730
Rep Power: 4 Dameon is on a distinguished road
The Click event is called from the message loop. Control will not return to the message loop until BackUp returns, and it's going to take a while. Since the form can not process messages until then, you cannot interact with it and it will shortly be reported as unresponsive as a result of the queue filling up. The best solution would be to move the backup operations to a separate thread. Look in to the BackgroundWorker component. You will of course need to be wary of synchronization issues.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270
Dameon is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
GOTO Sys. Tray Src. (code Snippet) Cipher Show Off Your Open Source Projects 1 Oct 17th, 2006 1:38 PM
Compiling Maverik 6.2 (from C) megamind5005 C 16 May 3rd, 2006 5:41 PM
visual basic pixel image comparison youngnoviceinneedofhelp Visual Basic 3 Mar 19th, 2006 1:57 PM
Jackpot game zorin Visual Basic 3 Jun 10th, 2005 1:19 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:26 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC