Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8777675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:19:59+00:00 2026-06-13T19:19:59+00:00

I have an MVC3 web app via which users upload text files to the

  • 0

I have an MVC3 web app via which users upload text files to the APP Data/Upload folder on a server. The requirement is to transfer these newly uploaded files to a folder on another server through FTP. I used a FileSystemWatcher in a Console Application which monitors this Upload folder. When a file is created fully, I transfer this newly generated file to the FTP folder.

My concern is that if multiple instances/users upload the file from the web application, how would my console application handle that scenario? For instance, do I need to have a queue and process the files on a different thread using for example the FileProcesser below?

public class FileProcessor
{
    private readonly Queue<string> files = new Queue<string>();
    private Thread thread;
    private readonly EventWaitHandle waitHandle = new AutoResetEvent(true);
    private static readonly object lockObject = new object();

    private volatile bool shouldStop = false;

    #region Helper methods

    private static bool IsFileUploadComplete(string path)
    {
        try
        {
            using (File.Open(path, FileMode.Open, FileAccess.Read, FileShare.None))
            {
                return true;
            }
        }
        catch (IOException)
        {
            return false;
        }
    }

    private void ProcessFile(string path)
    {
        // Check if Result file has been completely created / uploaded
        int maximumProcessRetries = 5;
        int delayBeforeRetry = 5000; //in milliseconds, 5 seconds

        int attempts = 0;

        while (true)
        {
            if (IsFileUploadComplete(path))
            {
                //1. Open in existing IE process on Instrument Workstation
                var p = new System.Diagnostics.Process();
                var s = new System.Diagnostics.ProcessStartInfo(Settings1.Default.RSLSM_URL);
                s.UseShellExecute = true;
                s.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
                p.StartInfo = s;
                p.Start();

                //2. Open in new IE process on Instrument workstation
                //System.Diagnostics.Process.Start("IEXPLORE.EXE", "www.yahoo.com");

                break;
            }
            attempts += 1;
            if (attempts >= maximumProcessRetries)
            {
                // Log error and send out notifications to RSLMS Database or Email RSLMS Admin group?
                break;
            }
            Thread.Sleep(delayBeforeRetry);
        }

        // Add any logic after the file has been processed
        //File.Delete(path);
    }

    private void Work()
    {
        while (!shouldStop)
        {
            string path = String.Empty;
            lock (lockObject)
            {
                if (files.Count > 0)
                {
                    path = files.Dequeue();
                }
            }

            if (!String.IsNullOrEmpty(path))
            {
                // Process the file
                ProcessFile(path);
            }
            else
            {
                // If no files are left to process then wait
                waitHandle.WaitOne();
            }
        }
    }

    #endregion

    #region Methods

    public void EnqueueFile(string path)
    {
        // Queue the file
        lock (lockObject)
        {
            files.Enqueue(path);
        }

        // Initialize and start the worker thread when the first file is queued
        // or when it has been stopped and thus terminated.
        if (thread == null || shouldStop)
        {
            thread = new Thread(new ThreadStart(Work));
            thread.Start();
        }
        // If the thread is waiting then start it
        else if (thread.ThreadState == ThreadState.WaitSleepJoin)
        {
            waitHandle.Set();
        }
    }

    public void StopProcessing()
    {
        shouldStop = true;
    }

    #endregion
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T19:20:01+00:00Added an answer on June 13, 2026 at 7:20 pm

    Your FileSystemWatcher is off running on its own thread and notifies your server code that something has happened (in your case a file was created).

    In the event args passed to your code it provides a list of files that were created and you process through that list of files. Any events raised during your process running will either be run on another thread or queued up depending on your locking strategy.

    Generally, though, when I get an event I get a list of all files from Directory.GetFiles and process that list. It guarantees that I don’t lose any files from the Watcher events.

    If this is going to run in production, you’ll want to get rid of that console application and make your process run as a Windows Service. A console application is too fragile and can be terminated by anyone that walks up to that console.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an MVC3 C#.Net web app. I have a requirement to display a
I have a requirement to implement user privilege elevation in an MVC3 web app,
I have an MVC3 web app which is effectively 'bolted on' to a classic
I have a C#.Net MVC3 web app and we use HTML.TextAreaFor() text areas for
I have an MVC3 C# .Net web app using IIS 6 on windows server
I have inherited an MVC3 C# .Net Web App which uses CkEditor. The ability
I have an MVC3 web app to which I want to start using whole
I have an MVC3 C# .Net web app. The app works fine when launching
I have a MVC3 project for running on the iPad in the web-app mode.
I have a C# .Net web app using MVC3. I am using Aspose to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.