Services

A QuickIO.NET service is an instance for processing multiple jobs. QuickIO.NET services offer multiple workers (Threads), to enable parallel processing for jobs.
As well, the priority that a job has, is observed during the processing.

By default QuickIO.NET offers three different services.

BackgroundService (online help link)

processes all jobs, until the queue is manually finalized or service is killed by user.
If no jobs is in queue all workers wait as long as no new elements is added to the queue. The cpu will not be charged in sleep-mode!


class Program
{
    const string dropDirectory = @"C:\transfer_test\dropfolder";
    const string pictureFolder = @"C:\transfer_test\pictures";
    const string movieFolder = @"C:\transfer_test\movies";
    protected static QuickIOTransferBackgroundService TransferBackgroundService = new QuickIOTransferBackgroundService( workerCount: 2, maxFileRetry: 5 );
    protected static FileSystemWatcher PictureWatcher = new FileSystemWatcher( dropDirectory, "*.png" );
    protected static FileSystemWatcher MovieWatcher = new FileSystemWatcher( dropDirectory, "*.mp4" );
    static void Main( string[ ] args )
    {
        PictureWatcher.Created += OnCreated_Picture;
        MovieWatcher.Created += OnCreated_MovieFile;
        TransferBackgroundService.Enqueued += OnJobEnqueued;
        TransferBackgroundService.Dequeued += OnJobDequeued;
        TransferBackgroundService.Requeued += JobRequeued;
        TransferBackgroundService.WorkerPickedJob += OnServiceWorkerPickedJob;
        TransferBackgroundService.WorkerCreated += OnServiceWorkerCreated;
        TransferBackgroundService.WorkerStarted += OnServiceWorkerStarted;
        TransferBackgroundService.WorkerPickedJob += OnServiceWorkerPickedJob;
        TransferBackgroundService.Started += OnFileCopyStarted;
        TransferBackgroundService.Progress += OnFileCopyProgress;
        TransferBackgroundService.Finished += OnFileCopyFinished;
        TransferBackgroundService.StartWorking( );
        Console.WriteLine( "Service is running." );
        Console.ReadKey( );
        // Ends on key input
    }
    /// <summary>
    /// worker started
    /// </summary>
    private static void OnServiceWorkerStarted( object sender, QuickIOTransferWorkerStartedEventArgs e )
    {
        Console.WriteLine( "[!] Worker Started. ID: " + e.WorkerID );
    }
    /// <summary>
    /// new worker
    /// </summary>
    static void OnServiceWorkerCreated( object sender, QuickIOTransferWorkerCreatedEventArgs e )
    {
        Console.WriteLine( "[!] New Worker. ID: " + e.WorkerID );
    }
    /// <summary>
    /// worker catched a job
    /// </summary>
    static void OnServiceWorkerPickedJob( object sender, QuickIOTransferWorkerPickedJobEventArgs e )
    {
        Console.WriteLine( "[!] Worker ID# " + e.WorkerID + " picked job: " + e.GetType( ) );
    }
    /// <summary>
    /// job was broken by an exception, but requred
    /// </summary>
    static void JobRequeued( object sender, QuickIOTransferJobRequeuedArgs e )
    {
        Console.WriteLine( "[JOB REQUEUED] Try: #" + e.Job.CurrentRetryCount + " - " + e.Job.GetType( ) + " failed: " + e.Exception.Message );
    }
    /// <summary>
    /// Report of job was taken from queue
    /// </summary>
    static void OnJobDequeued( object sender, QuickIOTransferJobDequeuedArgs e )
    {
        Console.WriteLine( "[JOB DEQUEUED] " + e.Job.GetType( ) );
    }
    /// <summary>
    /// Report for new jobs
    /// </summary>
    static void OnJobEnqueued( object sender, QuickIOTransferJobEnqueuedArgs e )
    {
        Console.WriteLine( "[NEW JOB] " + e.Job.GetType( ) );
    }
    /// <summary>
    /// Progress Report
    /// </summary>
    static void OnFileCopyStarted( object sender, QuickIOTransferFileCopyStartedArgs args )
    {
        Console.WriteLine( ">>>>>> STARTED " + args.SourcePath + " to " + args.TargetPath );
    }
    /// <summary>
    /// Progress Report
    /// </summary>
    static void OnFileCopyFinished( object sender, QuickIOTransferFileCopyFinishedArgs args )
    {
        Console.WriteLine( ">>>>>> FINISHED " + args.SourcePath + " to " + args.TargetPath + " - MB/s: " + ( args.BytesPerSecond / 1024.0 / 1024.0 ).ToString( "0.0" ) );
    }
    /// <summary>
    /// Progress Report
    /// </summary>
    static void OnFileCopyProgress( object sender, QuickIOTransferFileCopyProgressArgs args )
    {
        Console.WriteLine( "Transfering " + args.SourcePath + " to " + args.TargetPath + " - %: " + args.Percentage + " MB/s: " + ( args.BytesPerSecond / 1024.0 / 1024.0 ).ToString( "0.0" ) );
    }
    /// <summary>
    /// Copy movie file from drop folder to internal movie folder
    /// </summary>
    static void OnCreated_MovieFile( object sender, FileSystemEventArgs e )
    {
        var queueItem = new QuickIOTransferFileCopyJob( e.FullPath, movieFolder, overwrite: true );
        TransferBackgroundService.Add( queueItem );
    }
    /// <summary>
    /// Copy picture file from drop folder to internal picture folder
    /// </summary>
    static void OnCreated_Picture( object sender, FileSystemEventArgs e )
    {
        var queueItem = new QuickIOTransferFileCopyJob( e.FullPath, pictureFolder, overwrite: true, parentExistanceCheck: false );
        TransferBackgroundService.Add( queueItem );
    }
}

FileCopyService (online help link)

processes one or multile file copy jobs, until the queue is empty.
All workers will be terminated automatically after the end of all jobs.


class Program
{
    static void Main( string[ ] args )
    {
       const string sourceDirectory = @"C:\transfer_test\source";
       const string targetDirectory = @"C:\transfer_test\to";
       // search file
       var file = new QuickIOFileInfo( "file" );
       var service = new QuickIOTransferFileCopyService( file, targetDirectory, threadCount: 1, retryCount: 3, overwrite: true );
       
       //  Progress information
       service.Observer.FileCopyStarted += OnFileCopyStarted;
       service.Observer.FileCopyProgress += OnFileCopyProgress;
       service.Observer.FileCopyFinished += OnFileCopyFinished;
       service.Observer.FileCopyError += ObserverOnFileCopyError;
       // Same as (observer events are called first!):
       //service.FileCopyStarted += OnFileCopyStarted;
       //service.FileCopyProgress += OnFileCopyProgress;
       //service.FileCopyFinished += OnFileCopyFinished;
       //service.FileCopyError += ObserverOnFileCopyError;
       
       // Start progress
       service.Start( ); // Blocks thread until finished
       
       Console.WriteLine( "Finished" );
       Console.ReadKey( );
    }
    private static void ObserverOnFileCopyError( object sender, QuickIOTransferFileCopyErrorEventArgs e )
    {
       Console.WriteLine( "Error: " + e.SourcePath + " to " + e.TargetPath + ": " + e.Exception.Message );
    }
    private static void OnFileCopyStarted( object sender, QuickIOTransferFileCopyStartedEventArgs e )
    {
       Console.WriteLine( "Started: " + e.SourcePath + " to " + e.TargetPath + " (Bytes: " + e.TotalBytes + ")" );
    }
    private static void OnFileCopyFinished( object sender, QuickIOTransferFileCopyFinishedEventArgs e )
    {
       Console.WriteLine( "Finished: " + e.SourcePath + " - MB/s: " + ( e.BytesPerSecond / 1024.0 / 1024.0 ).ToString( "0.0" ) );
    }
    private static void OnFileCopyProgress( object sender, QuickIOTransferFileCopyProgressEventArgs e )
    {
       Console.WriteLine( "Progress: " + e.SourcePath + " - %: " + e.Percentage + " MB/s: " + ( e.BytesPerSecond / 1024.0 / 1024.0 ).ToString( "0.0" ) );
    }
}

DirectoryCopyService (online help link)

copies a complete directory. Searches all content and creates multiple file copy jobs.
All workers will be terminated automatically after the end of all jobs.


static void Main( string[ ] args )
{
    const string sourceDirectory = @"C:\transfer_test\source";
    const string targetDirectory = @"C:\transfer_test\to";
    // With observer
    IQuickIOTransferObserver observer = new QuickIOTransferObserver( );
    var service = new QuickIOTransferDirectoryCopyService( observer, new QuickIODirectoryInfo( sourceDirectory ), targetDirectory, threadCount: 1, retryCount: 3, searchOption: SearchOption.AllDirectories, overwrite: true );
    // or without overload, to use default internal observer
    // var service = new QuickIOTransferDirectoryCopyService( new QuickIODirectoryInfo( sourceDirectory ), targetDirectory, threadCount: 1, retryCount: 3, searchOption: SearchOption.AllDirectories, overwrite: true );
    //  Progress information
    service.Observer.DirectoryCreationError += ObserverOnDirectoryCreationError;
    service.Observer.FileCopyStarted += OnFileCopyStarted;
    service.Observer.FileCopyProgress += OnFileCopyProgress;
    service.Observer.FileCopyFinished += OnFileCopyFinished;
    service.Observer.FileCopyError += ObserverOnFileCopyError;
    // Same as (observer events are called first!):
    service.FileCopyStarted += OnFileCopyStarted;
    service.FileCopyProgress += OnFileCopyProgress;
    service.FileCopyFinished += OnFileCopyFinished;
    service.FileCopyError += ObserverOnFileCopyError;
    // Start progress
    service.Start( ); // Blocks thread until finished
    Console.WriteLine( "Finished" );
    Console.ReadKey( );
}
private static void ObserverOnDirectoryCreationError( object sender, QuickIOTransferDirectoryCreationErrorEventArgs e )
{
    Console.WriteLine( "Error: Dir create '" + e.TargetPath + "' failed: " + e.Exception.Message );
}
private static void ObserverOnFileCopyError( object sender, QuickIOTransferFileCopyErrorEventArgs e )
{
    Console.WriteLine( "Error: " + e.SourcePath + " to " + e.TargetPath + ": " + e.Exception.Message );
}
private static void OnFileCopyStarted( object sender, QuickIOTransferFileCopyStartedEventArgs e )
{
    Console.WriteLine( "Started: " + e.SourcePath + " to " + e.TargetPath + " (Bytes: " + e.TotalBytes + ")" );
}
private static void OnFileCopyFinished( object sender, QuickIOTransferFileCopyFinishedEventArgs e )
{
    Console.WriteLine( "Finished: " + e.SourcePath + " - MB/s: " + ( e.BytesPerSecond / 1024.0 / 1024.0 ).ToString( "0.0" ) );
}
private static void OnFileCopyProgress( object sender, QuickIOTransferFileCopyProgressEventArgs e )
{
    Console.WriteLine( "Progress: " + e.SourcePath + " - %: " + e.Percentage + " MB/s: " + ( e.BytesPerSecond / 1024.0 / 1024.0 ).ToString( "0.0" ) );
}