Deleting Files With Events

While working on the dotNetTips Dev Cleaner utility, I wanted to make the deletion of files even faster. While writing this utility I found and worked on speed issues, almost all relating to updating the user interface. So to decouple the deleting from the UI I decided to add a new feature to the dotNetTips.Utility open source project.

Processor Class

I added a new class in the dotNetTips.Utility.IO namespace called Processor. The purpose of this class is to copy, move and delete files while firing events that can be used to update the UI. Unlike other methods I have used in other frameworks, if an exception occurs, it fires an event and keeps processing.

First I created the event:

''' <summary>
''' Occurs when processor processes a file or folder.
''' </summary>
Event Processed As EventHandler(Of ProgressEventArgs)
 
''' <summary>
''' Handles the <see cref="E:Processed/> event.
''' </summary>
''' <param name="e">The <see cref="ProgressEventArgs"/> 
                    instance containing the event 
                    data.</param>
Protected Overridable Sub OnProcessed(e As ProgressEventArgs)
    RaiseEvent Processed(Me, e)
End Sub

The event above is called by the code below:

''' <summary>
  ''' Deletes file list.
  ''' </summary>
  ''' <param name="files">The file list to delete.</param>
  ''' <returns>System.Int32 with the number of files that were 
               successfully deleted.</returns>
  ''' <remarks>Use the <seealso cref="Processed">Processed
               </seealso> event to find out if file deletion 
               succeeded or failed.</remarks>
  Public Function DeleteFiles(files As IEnumerable(Of FileInfo)) 
                    As Integer
      Encapsulation.TryValidateParam(files, NameOf(files))
 
      Dim successCount = 0
 
      For Each tempFile In files.AsParallel
          If tempFile.Exists Then
              Try
                  tempFile.Delete()
 
                  successCount += 1
 
                  OnProcessed(New ProgressEventArgs With 
                              {.Name = tempFile.FullName, 
                               .ProgressState = 
                                 ProgressState.Deleted, 
                               .Size = tempFile.Length})
 
              Catch ex As Exception
                  OnProcessed(New ProgressEventArgs With 
                             {.Name = tempFile.FullName, 
                              .ProgressState = 
                                ProgressState.Error, 
                              .Size = tempFile.Length, 
                              .Message = ex.Message})
              End Try
 
          Else
              OnProcessed(New ProgressEventArgs With 
                         {.Name = tempFile.FullName, 
                          .ProgressState = 
                            ProgressState.Error, 
                          .Size = tempFile.Length, 
                          .Message = Resources.FileNotFound})
          End If
      Next
 
      Return successCount
  End Function

This new class helped my utility go from deleting 1K files per second to up to around 2K per second!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s