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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:42:47+00:00 2026-05-26T09:42:47+00:00

I use multi threading to copy file to another place.I from stream needed byte

  • 0

I use multi threading to copy file to another place.I from stream needed byte array and dispose stream.at this example i use 7 threads to copy 3gb file.1st thread can get byte array,but at 2nd thread occurs exception ‘System.OutOfMemoryException’

public void Begin()
{
    FileStream stream = new FileStream(pathToFile, FileMode.Open);
    stream.Position = (threNmb - 1) * 536870912;
    BinaryReader reader = new BinaryReader(stream);
    for (long i = 0; i < (length); i++)
    {
        source.Add(reader.ReadByte());//gives exception at i=134217728
    }
    reader.Dispose();
    reader.Close();
    stream.Dispose();
    stream.Close();
}
  • 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-05-26T09:42:48+00:00Added an answer on May 26, 2026 at 9:42 am

    It looks like you’re using a List<byte>. That’s going to be a very inefficient way of copying data, and you’re probably making it less efficient by using multiple threads. Additionally, if you’re using a single list from multiple threads, your code is already broken – List<T> isn’t thread-safe. Even if it were, you’d be mixing the data from the different threads, so you wouldn’t be able to reassemble the original data. Oh, and by not using using statements, if an exception is thrown you’re leaving file handles open. In other words, I’m advising you to completely abandon your current approach.

    Instead, copying a file from one place to another (assuming you can’t use File.Copy for some reason), should basically be a case of:

    • Open stream to read
    • Open stream to write
    • Allocate buffer (e.g. 32K)
    • Repeatedly:
      • Read from the “input” stream into the buffer, noting how much data the call actually read
      • Write the data you’ve just read into the output stream
      • Loop until your “Read” indicates the end of the input stream
    • Close both streams (with using statements)

    There’s no need to have everything in memory. Note that in .NET 4 this is made even easier with the Stream.CopyTo method. which does the third and fourth steps for you.

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

Sidebar

Related Questions

This is my c++ struct (Use Multi-Byte Character Set) typedef struct hookCONFIG { int
Possible Duplicate: How can one use multi threading in php applications Does anybody know
I'm doing some multi-threading and use AutoResetEvents and ManualResetEvents do control my main -
I am having a problem with multi threading in C#. I use an event
I have a program where I use boost::threads for multi threading. Unfortunately drd (
I'm very new to multi-threading and for some reason this class is giving me
I'm modifying an application written in C# that makes heavy-use of multi-threading to play
Is it possible to use multi-threading in a .NET COM callable wrapper DLL assembly?
I use curl of ruby gem curb to fetch multi urls under multi-threading env,
this is my first adventure into multi threading and I think I am missing

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.