Click here to Skip to main content
15,887,392 members
Home / Discussions / C#
   

C#

 
GeneralRe: Textbox updation Pin
Avinash.Banda14-Nov-16 0:43
Avinash.Banda14-Nov-16 0:43 
GeneralRe: Textbox updation Pin
OriginalGriff14-Nov-16 0:45
mveOriginalGriff14-Nov-16 0:45 
GeneralRe: Textbox updation Pin
Avinash.Banda14-Nov-16 0:52
Avinash.Banda14-Nov-16 0:52 
GeneralRe: Textbox updation Pin
OriginalGriff14-Nov-16 1:03
mveOriginalGriff14-Nov-16 1:03 
GeneralRe: Textbox updation Pin
Avinash.Banda14-Nov-16 1:04
Avinash.Banda14-Nov-16 1:04 
QuestionCan I send Push Notifications to mobile phones from Windows Forms Application? Pin
Onur ERYILMAZ13-Nov-16 23:58
Onur ERYILMAZ13-Nov-16 23:58 
AnswerRe: Can I send Push Notifications to mobile phones from Windows Forms Application? Pin
Eddy Vluggen14-Nov-16 3:51
professionalEddy Vluggen14-Nov-16 3:51 
QuestionFile Transfer Queue Architecture Pin
Kevin Marois10-Nov-16 6:16
professionalKevin Marois10-Nov-16 6:16 
I have an app that transfers files to a server via FTP. This class doesn't actually do the Ftp'ing, it just maintains a queue of files to be sent.

Some requirements:

1) No more than 3 files can be FTP'd at one time.
2) The appearance of a file in the target folder should start the process.
3) When a file is done being FTP'd, the next file should automatically start.

So I came up with this FTPQueue class[^]. I would appreciate any feedback.

Thanks
public static class FTPQueue
{
    /// <summary>
    /// The maximum number of concurrent file uploads
    /// </summary>
    private const int MAX_FILE_UPLOADS = 3;
    /// <summary>
    /// The number of files being currently uploaded
    /// </summary>
    private static int _filesBeingUploaded = 0;
    /// <summary>
    /// Maintains a FIFO list of files to be uploaded
    /// </summary>
    private static Queue _fileQueue = new Queue();
    /// <summary>
    /// Maintains a list of the names of the files currently being
    /// uploaded. This prevents the process from attempting to upload
    /// the same multiple times
    /// </summary>
    private static List<string> _filesInProgress = new List<string>();
    /// <summary>
    /// Called from the FileSystemWatcher when file(s) are received in the ap's target folder
    /// </summary>
    /// <param name="fileName">Name of the received file.</param>
    public static void FileReceived(string fileName)
    {
        Console.WriteLine("Received file {0}", fileName);

        // Add the file recieved to the queue of files
        _fileQueue.Enqueue(fileName);

        UploadNextFile();
    }
    /// <summary>
    /// Uploads the next file.
    /// </summary>
    private static async void UploadNextFile()
    {
        // Get the next file from the list
        if (_fileQueue.Count == 0)
        {
            return;
        }
        string fileName = (string)_fileQueue.Peek();

        if (!string.IsNullOrEmpty(fileName))
        {
            // If we've reached the max number of uploads tyhen return
            if (_filesBeingUploaded >= MAX_FILE_UPLOADS)
            {
                return;
            }

            // See if the file is a already being transferred, and if so, exit
            if (_filesInProgress.Any(x => x == fileName))
            {
                return;
            }

            // Keep track of the number of files being uploaded
            _filesBeingUploaded++;
            Console.WriteLine("Files being uploaded {0}", _filesBeingUploaded);

            // Create the upload task
            Task<bool> upload = TransferFile(fileName);

            // Wait for the file upload to complete
            bool result = await upload;

            // Call the completed method
            Console.WriteLine("Files {0} upload complete", _filesBeingUploaded);

            FileUploadComplete(fileName, result);
        }
    }
    /// <summary>
    /// Performs a file transfer operation
    /// </summary>
    /// <param name="fileName">Name of the file to transfer.</param>
    /// <returns></returns>
    private static async Task<bool> TransferFile(string fileName)
    {
        Console.WriteLine("Tranfering file {0}", fileName);

        // 10 second delay to simulate a long running FTP operation
        await Task.Delay(10000);

        // Set return value from FTP process
        bool result = true;

        return result;
    }
    /// <summary>
    /// Called when a file upload is complete.
    /// </summary>
    /// <param name="fileName">Name of the file uploaded.</param>
    /// <param name="result">True if the file upload was sucesseful, otherwise false.</param>
    private static void FileUploadComplete(string fileName, bool result)
    {
        // If the file was FTP'd ok, the remove it from the list
        if (result)
        {
            Console.WriteLine(string.Format("File {0} upload complete", fileName));

            if (_fileQueue.Count > 0)
            {
                // Remove the file name from the queue and InProgress list
                Console.WriteLine("Removing file {0} from the queue", fileName);

                _fileQueue.Dequeue();
                _filesInProgress.Remove(fileName);
            }

            // Decrement the number of files being uploaded
            _filesBeingUploaded--;
            Console.WriteLine("Files being uploaded {0}", _filesBeingUploaded);
        }
        else
        {
            Console.WriteLine(string.Format("File {0} upload failed", fileName));
        }

        // Start the next file
        UploadNextFile();
    }
}

For testing I ran it from a simple Console app:
class Program
{
    private static Timer _timer;

    static void Main(string[] args)
    {
        _timer = new Timer();
        _timer.Interval = 3000;
        _timer.Elapsed += _timer_Elapsed;
        _timer.Enabled = true;

        Console.ReadLine();
    }

    // The timer creates a dummy file to mock a file being dropped into the target folder. In
    // the real app a FileSystemWatcher would handle this
    private static void _timer_Elapsed(object sender, ElapsedEventArgs e)
    {
        var fileName = Path.GetRandomFileName();
        Console.WriteLine("Created new file {0}", fileName);

        FTPQueue.FileReceived(fileName);
    }
}
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.

QuestionRe: File Transfer Queue Architecture Pin
Gerry Schmitz10-Nov-16 6:41
mveGerry Schmitz10-Nov-16 6:41 
AnswerRe: File Transfer Queue Architecture Pin
Kevin Marois10-Nov-16 6:42
professionalKevin Marois10-Nov-16 6:42 
GeneralRe: File Transfer Queue Architecture Pin
Gerry Schmitz10-Nov-16 7:04
mveGerry Schmitz10-Nov-16 7:04 
GeneralRe: File Transfer Queue Architecture Pin
Kevin Marois10-Nov-16 7:22
professionalKevin Marois10-Nov-16 7:22 
GeneralRe: File Transfer Queue Architecture Pin
Gerry Schmitz10-Nov-16 7:33
mveGerry Schmitz10-Nov-16 7:33 
GeneralRe: File Transfer Queue Architecture Pin
Kevin Marois10-Nov-16 7:38
professionalKevin Marois10-Nov-16 7:38 
GeneralRe: File Transfer Queue Architecture Pin
Gerry Schmitz10-Nov-16 8:08
mveGerry Schmitz10-Nov-16 8:08 
GeneralRe: File Transfer Queue Architecture Pin
Dave Kreskowiak10-Nov-16 11:02
mveDave Kreskowiak10-Nov-16 11:02 
GeneralRe: File Transfer Queue Architecture Pin
Kevin Marois10-Nov-16 11:06
professionalKevin Marois10-Nov-16 11:06 
GeneralRe: File Transfer Queue Architecture Pin
Dave Kreskowiak10-Nov-16 15:30
mveDave Kreskowiak10-Nov-16 15:30 
GeneralRe: File Transfer Queue Architecture Pin
Kevin Marois11-Nov-16 5:42
professionalKevin Marois11-Nov-16 5:42 
GeneralRe: File Transfer Queue Architecture Pin
Gerry Schmitz11-Nov-16 6:30
mveGerry Schmitz11-Nov-16 6:30 
GeneralRe: File Transfer Queue Architecture Pin
Kevin Marois11-Nov-16 6:33
professionalKevin Marois11-Nov-16 6:33 
GeneralRe: File Transfer Queue Architecture Pin
Gerry Schmitz11-Nov-16 6:44
mveGerry Schmitz11-Nov-16 6:44 
GeneralRe: File Transfer Queue Architecture Pin
Dave Kreskowiak11-Nov-16 7:22
mveDave Kreskowiak11-Nov-16 7:22 
AnswerRe: File Transfer Queue Architecture Pin
Richard Deeming10-Nov-16 7:47
mveRichard Deeming10-Nov-16 7:47 
GeneralRe: File Transfer Queue Architecture Pin
Kevin Marois10-Nov-16 7:49
professionalKevin Marois10-Nov-16 7:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.