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 9148611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:19:26+00:00 2026-06-17T11:19:26+00:00

I am uploading large files via Silverlight and have implemented a chunking function. It

  • 0

I am uploading large files via Silverlight and have implemented a chunking function. It works fine, however, if I upload three large (500mb) files in a row I still get out of memory exceptions. Below is my code, can you spot anything that is missing?

const int _ReadSize = 2097152;

byte[] _Buffer = new byte[_ReadSize];

    /// <summary>
    /// This method will do the initial read and write and send off the first chunk of data.
    /// This is where the filestream is opened from the file info.
    /// It also passes a set of parameters to the next call. These are:
    /// * bytesRead - the number of bytes that was actually read from the file with stream.Read
    /// * stream - This is the filestream
    /// * offset - This is the updated offset that has been moved to the position in the stream we are currently at
    /// </summary>
    void DoWork()
    {
        FileStream stream = _SelectedFile.OpenRead();

        ServerAvailable = false;

        bool startRead = true;

        int bytesRead = 0;

        bytesRead = stream.Read(_Buffer, 0, _ReadSize);

        int offset = bytesRead;

        List<object> args = new List<object>();
        args.Add(bytesRead);
        args.Add(stream);
        args.Add(offset);

        IDataManipulationService client = new DataManipulationServiceClient();
        client.BeginUploadLargeFile(_Buffer, (int)_SelectedFile.Length, FileName, startRead, offset - bytesRead, bytesRead, FinishedUploadPiece, args);
    }

    /// <summary>
    /// This method is called once the previous call to the web server has been completed.
    /// It will read the next chunk of the file and send that through to the web server next.
    /// If 0 bytes were read from the previous read on the stream; it will do the following:
    /// - Close the file stream
    /// - Dispose the file stream
    /// - set the FileInfo to null
    /// - Reset the FileSize, UploadProgress and FileName variables to default values
    /// - Make the buttons available for use
    /// </summary>
    /// <param name="result">The result contains the information about the outcome of the previous call. This also contains the args parameter sent through with the previous call.</param>
    void FinishedUploadPiece(IAsyncResult result)
    {
        if (result.IsCompleted)
        {
            List<object> args = (List<object>)result.AsyncState;

            int bytesRead = (int)args[0];
            FileStream stream = (FileStream)args[1];
            int offset = (int)args[2];

            if (bytesRead != 0)
            {
                UploadProgress += bytesRead;

                if (UploadProgress == FileSize)
                {
                    FileSize = 0;
                    UploadProgress = 0;
                    FileName = String.Empty;

                    ServerAvailable = true;
                    stream.Close();
                    stream.Dispose();
                    _SelectedFile = null;
                }
                else
                {
                    bytesRead = stream.Read(_Buffer, 0, _ReadSize);

                    offset += bytesRead;

                    args = new List<object>();

                    args.Add(bytesRead);
                    args.Add(stream);
                    args.Add(offset);

                    IDataManipulationService client = new DataManipulationServiceClient();
                    client.BeginUploadLargeFile(_Buffer, (int)_SelectedFile.Length, FileName, false, offset - bytesRead, bytesRead, FinishedUploadPiece, args);
                }
            }
        }
    }

To clarify some stuff:
_SelectedFile is of type FileInfo and I create a new connection to the server each time I want to send data through but have also tried having a global injected connection.

  • 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-17T11:19:27+00:00Added an answer on June 17, 2026 at 11:19 am

    I would try to refactor the code to use Error Codes and not pass the stream around.

    void DoWorkAsync()
    {
        IDataManipulationService client = new DataManipulationServiceClient();
    
        try
        {
            using(FileStream stream = _SelectedFile.OpenRead())
            {
                int streamLength = (int)_SelectedFile.Length
                ServerAvailable = false;
    
                bool startRead = true;
    
                int bytesRead;
                int offset = 0;
    
                while((bytesRead = stream.Read(_Buffer, 0, _ReadSize)) > 0)
                {
                    offset += bytesRead;
                    UploadState state = await client.UploadChunk(_Buffer, streamLength, FileName, offset, bytesRead)
    
                    if(state == UploadState.Error)
                        //error Handling
    
                    if(state == UploadState.Corrupt)
                        //Retry send
    
                    if(state == UploadState.Success)
                        continue;
                }
            }
        }
        catch(Exception e) {} //TODO: Replace Exception with some meaningful exception.
        finally
        {
            ServerAvailable = true;
        }
    }
    
    enum UploadState
    {
        Success,
        Error,
        Corrupt
    }
    

    Alternatives to async/await:

    See What should I do to use Task<T> in .NET 2.0? for a a lightweight implementation of async/await.

    Reactive Extensions will provide an implementation of System.Threading.dll which contains Task or you can use NuGet.

    Here is an example implementation:

    void DoWork()
    {
        IDataManipulationService client = new DataManipulationServiceClient();
    
        try
        {
            using(FileStream stream = _SelectedFile.OpenRead())
            {
                int streamLength = (int)_SelectedFile.Length
                ServerAvailable = false;
    
                bool startRead = true;
    
                int bytesRead;
                int offset = 0;
    
                while((bytesRead = stream.Read(_Buffer, 0, _ReadSize)) > 0)
                {
                    offset += bytesRead;
                    Task<UploadState> task = client.UploadChunk(_Buffer, streamLength, FileName, offset, bytesRead);
    
                    while(true)
                    {
                        if(task.IsCompleted)
                        {
                            UploadState state = task.Result;
                            break;
                        }
    
                        if(task.IsFaulted)
                        {
                            var exception = task.Exception;
                            //Deal with errors
                            break;
                        }
                    }
    
                    if(state == UploadState.Error)
                        //error Handling
    
                    if(state == UploadState.Corrupt)
                        //Retry send
    
                    if(state == UploadState.Success)
                        continue;
                }
            }
        }
        catch(Exception e) {} //TODO: Replace Exception with some meaningful exception.
        finally
        {
            ServerAvailable = true;
        }
    }
    
    enum UploadState
    {
        Success,
        Error,
        Corrupt
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to upload large files in Silverlight w/o resorting to the chunked
I am trying to following this web blog on uploading large files using the
I have been using Indy to transfers files via FTP for years now but
I am currently uploading files in ActionScript 3 using the upload() method of the
I am uploading large files to an ASP.NET server using a standard HTML <input>
I am receiving a 500 Internal Server Error when trying to upload large files
I am using Bottle for uploading rather large files. The idea is that when
I am uploading a large file by SFTP over Jsch. During the upload process,
I have a application, which accepts xml files, upon uploading a invalid xml file
I am wondering what is the general consensus for uploading moderately large files. I

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.