public class QuickIOTransferBackgroundService : QuickIOTransferServiceBase
Instance for the continuous transfer of files; for example as a service.
Sort incoming files by an always watching file service
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 );
}
}
QuickIOTransferBackgroundService(IQuickIOTransferObserver observer, Int32 workerCount, Int32 maxFileRetry, Boolean autostart)
Creates an instance for QuickIOTransferBackgroundService with specified observer
public void QuickIOTransferBackgroundService(IQuickIOTransferObserver observer, Int32 workerCount, Int32 maxFileRetry, Boolean autostart)
Parameters
observer
IQuickIOTransferObserverObserver for monitoring
workerCount
Int32maxFileRetry
Int32autostart
Booleantrue to auto start. false to start service by using
QuickIOTransferBackgroundService(Int32 workerCount, Int32 maxFileRetry, Boolean autostart)
Creates an instance for QuickIOTransferBackgroundService with default observer
public void QuickIOTransferBackgroundService(Int32 workerCount, Int32 maxFileRetry, Boolean autostart)
Parameters
workerCount
Int32maxFileRetry
Int32autostart
Booleantrue to auto start. false to start service by using
Add(QuickIOTransferFileCopyJob queueItem)
Adds an item to the queue. null will not be insered and returns false. If CompleteAdding is called the return value is false, too
public void Add(QuickIOTransferFileCopyJob queueItem)
Parameters
queueItem
QuickIOTransferFileCopyJobItem to add
Returns
true on add; false if not
AddWorker(Int32 count)
Adds a new worker to the service. Worker will be created and started instantly.
public void AddWorker(Int32 count)
Parameters
count
Int32Must be 1 or greater
Remarks
It's not recommended to use more workers than the count of useable CPU cores.
RemoveWorker(Int32 count)
Remove workers from the service.
public void RemoveWorker(Int32 count)
Parameters
count
Int32Must be 1 or greater
Start()
Starts the service if not started yet
public Boolean Start()
WaitForFinish()
Joins all threads and blocks until all threads and queue items are completed. Queue has to be completed.
public void WaitForFinish()