Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Visual Basic .NET (http://www.programmingforums.org/forum19.html)
-   -   Preventing Timeout Message During Long Processes (http://www.programmingforums.org/showthread.php?t=13474)

PhilBon Jul 4th, 2007 4:30 PM

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:
:

  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.

melbolt Jul 5th, 2007 2:01 PM

maybe try putting it in a new thread and see what happens

PhilBon Jul 5th, 2007 2:30 PM

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?

Dameon Jul 5th, 2007 3:12 PM

Is this function running on the main thread (it is if you didn't create any) in a WinForms app?

PhilBon Jul 5th, 2007 3:46 PM

1 Attachment(s)
it's a windows from, here is all the code:
:

  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


Dameon Jul 5th, 2007 7:20 PM

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.


All times are GMT -5. The time now is 2:36 AM.

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