Xceed .NET Libraries Documentation
Welcome to Xceed Data Manipulation Compoents for .NET and .NET Standard / Basic Concepts / Events / ByteProgression event

    ByteProgression event

    The ByteProgression event is raised while a list of FileSystemItem objects is being processed, for every 64k of bytes processed. 

    Keep in mind that using the ByteProgression event increases the time it takes to process the files. The reason behind this is that the data associated with the file will be scanned twice. The first time the files are scanned, information regarding the files will be accumulated ( filename, size, etc.. ). This is to allow accurate progress information to be returned during the actual processing of the files.

    Purpose

    The main purpose of the ByteProgression event is to provide feedback to the user during lengthy operations. For example, if we wanted to delete a folder and all it's sub-directories, the process could take a while and the application could appear to be frozen. To prevent this behavior, you can update a progress bar in the event handler for the ByteProgression event.

    Basic steps - C#

    To subscribe to the ByteProgression event, the following steps must be performed:

    • Create a reference to a FileSystemEvents object. 

    • Subscribe to the ByteProgression event of the FileSystemEvents object using the ByteProgressionEventHandler delegate class. 

    • Create a new method that will handle the events that are raised. For the purposes of this example, we will call the method OnByteProgression. 

    • Place the desired code in the newly created event handler.

    Basic steps - VB.NET

    To subscribe to the ByteProgression event, the following steps must be performed:

    • Create a reference to a FileSystemEvents object using the WithEvents keyword. 

    • Select the ByteProgression event from the list of available methods in the newly instantiated FileSystemEvents object. This is done in the same manner as, for example, adding the DoubleClick event of a ListBox.
      You can also subscribe to the event using the AddHandler/AddressOf statement. 

    • Place the desired code in the newly added event handler.

    Demonstration

    This example demonstrates how to delete a folder located on disk and display progress information during the operation.

    VB.NET Copy Code

    Imports Xceed.FileSystem

    Dim WithEvents fiEvents As New FileSystemEvents()
    Dim folder As New DiskFolder("c:\temp")

    folder.Delete( fiEvents, Nothing )

    Private Sub fiEvents_ByteProgression(ByVal sender As Object, _
                                          ByVal e As Xceed.FileSystem.ByteProgressionEventArgs) Handles fiEvents.ByteProgression

      ListBox1.Items.Add("Processed " + e.CurrentFileBytes.Processed + _
                         " bytes of " + e.CurrentFileBytes.Total + _
                         " on " + e.CurrentItem.Name)
    End Sub

    C# Copy Code
    using Xceed.FileSystem;
     
    FileSystemEvents fiEvents = new FileSystemEvents();
    fiEvents.ByteProgression += new ByteProgressionEventHandler( OnByteProgression );
     
    DiskFolder folder = new DiskFolder( @"c:\temp" );
    folder.Delete( fiEvents, null );
     
    //This method will handle the ByteProgression events that are raised.
    public static void OnByteProgression( object sender, ByteProgressionEventArgs e )
    {
       Console.WriteLine( "Processed {0} bytes of {1} on {2}.",
                          e.CurrentFileBytes.Processed,
                              e.CurrentFileBytes.Total,
                              e.CurrentItem.Name ); 
    }
     
    //If you no longer wish to handle the events that are raised,
    //you can unsubscribe from the event notifications by doing the following:
     
    fiEvents.ByteProgression -= new ByteProgressionEventHandler( OnByteProgression );