Synchronous file uploading and processing

Feb 23, 2010 at 8:31 PM

Could be a crazy request, but...

How could I modify the code to upload and process the files synchronously?

I have set the max upload parameter to 1, but the processing still happens asynchronously. 

Thank you in advance

Feb 23, 2010 at 9:36 PM

That would not be possible with Silverlight.  Silverlight only allows asynchronous communication with your web server.

Though that seems like a "limitation" imposed on you by Silverlight, in this particular case at least, it is not much of a problem.  File uploading is something that should arguably never be done synchronously -- it can take a long and/or unpredictable amount of time.  It would be a bad user experience to freeze the UI while a file upload is in progress.

Feb 23, 2010 at 10:10 PM

I figured out the answer to my problem. First, let me modify my question:

How can I process the files synchronously after they have been uploaded?


I simply added "lock (typeof(IUploadedFileProcessor))" around the code inside my ProcessFile method.

Thank you for your help and for the control. It has been a huge help



Feb 23, 2010 at 11:43 PM

If you would only like the server to process one file at a time, probably because you are accessing a shared resource on the server, a simple approach would be to use a lock in your custom file processor.

    public class ExampleFileProcessor : IUploadedFileProcessor
        private static object _processingLock = new object();

        #region IUploadedFileProcessor Members

        public void ProcessFile(HttpContext Context, string FileGuid, string FileName, string ContextParam)
            string sandboxPath = Path.Combine(Sandbox.UploaderControlSandboxPath, FileGuid);

            using (FileStream fs = File.OpenRead(sandboxPath))
                // this code should only be entered by one thread at a time
                lock (_processingLock)
                    // do something useful to the file

            // for testing, this example just deletes the file from the sandbox after it has been uploaded; comment
            // out this line if you want to verify that files are being uploaded correctly while testing