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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:54:30+00:00 2026-06-09T15:54:30+00:00

here is my code: using System; using System.Collections; using System.Collections.Generic; using System.Collections.Concurrent; using System.Linq;

  • 0

here is my code:

using System;

using System.Collections;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Linq;

using System.Threading;
using System.Threading.Tasks;

using System.IO;
using System.IO.MemoryMappedFiles;

namespace CopyFile
{
class Program
{
    static void Main()
    {
        long length = 0;
        byte[] buffer;
        string source_path, dest_path;
        source_path = Console.ReadLine();
        dest_path = Console.ReadLine();
        FileInfo fi = new FileInfo(source_path);
        length = (int)fi.Length;
        // Create disk file
        using (FileStream fs = File.Create(dest_path))
        {
            fs.Close();
        }
        // Create unnamed MMF
        var mmf1 = MemoryMappedFile.CreateFromFile(source_path, FileMode.OpenOrCreate, null, length);
        // Create reader to MMF
        var reader = mmf1.CreateViewAccessor(0, length, MemoryMappedFileAccess.Read);
        // Create unnamed MMF
        var mmf2 = MemoryMappedFile.CreateFromFile(dest_path, FileMode.Create, null, length);
        // Create writer to MMF
        var writer = mmf2.CreateViewAccessor(0, length, MemoryMappedFileAccess.Write);

        int read_block = int.Parse(Math.Min(length, 512 * 1024).ToString());//4k
        int end_read_block = int.Parse(length.ToString()) % read_block;

        int[] offset_array = new int[int.Parse((length - read_block).ToString()) / read_block];
        for (int offset = 0,i=0; i < int.Parse((length - read_block).ToString()) / read_block; i++,offset += read_block)
        {
            offset_array[i] = offset;
        }

        /*
        Parallel.ForEach<int>(offset_array, offset =>
        {
            // Read from MMF
            buffer = new byte[read_block];
            reader.ReadArray<byte>(offset, buffer, 0, read_block);
            // Write to MMF
            writer.WriteArray<byte>(offset, buffer, 0, read_block);
        });
         */
        foreach (int offset in offset_array)
        {
            // Read from MMF
            buffer = new byte[read_block];
            reader.ReadArray<byte>(offset, buffer, 0, read_block);
            // Write to MMF
            writer.WriteArray<byte>(offset, buffer, 0, read_block);
        }

        buffer = new byte[end_read_block];
        reader.ReadArray<byte>(length - end_read_block, buffer, 0, end_read_block);
        // Write to MMF
        writer.WriteArray<byte>(length - end_read_block, buffer, 0, end_read_block);

    }
}
}

i try to copy one file and paste it in another location
it is working

but when i try to use Parallel.foreach or Parallel.for when it copy one file
copied file is different with source file

(I commented Parallel.foreach section)

i can’t understand why

thanks

  • 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-09T15:54:32+00:00Added an answer on June 9, 2026 at 3:54 pm

    In your code;

    Parallel.ForEach<int>(offset_array, offset =>
    {
        // Read from MMF
        buffer = new byte[read_block];
        reader.ReadArray<byte>(offset, buffer, 0, read_block);
        // Write to MMF
        writer.WriteArray<byte>(offset, buffer, 0, read_block);
    });
    

    buffer is a shared variable, so all threads will share it. For example, thread 1 will assign buffer, read from the file, thread 2 will reassign buffer and read the file, thread 1 will write the contents of thread 2’s buffer.

    To make it work better, have buffer be a local variable in the loop, a’la;

    Parallel.ForEach<int>(offset_array, offset =>
    {
        // Read from MMF
        byte[] buffer = new byte[read_block];
        reader.ReadArray<byte>(offset, buffer, 0, read_block);
        // Write to MMF
        writer.WriteArray<byte>(offset, buffer, 0, read_block);
    });
    

    That will allow all threads to have their own, local, buffer.

    Looking at the documentation though makes things look bleak, any instance method (ReadArray/WriteArray) are not guaranteed to be thread safe. In other words, even if you fix the buffer problem, there is no guarantee that things will work.

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

Sidebar

Related Questions

here's the code: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using
Any help? Here's my code: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using
here is a code : using System; using Nemerle.Collections; using Nemerle.Text; //using Nemerle.Utility; using
The following is my code : using System; using System.Collections.Generic; using System.Text; using System.CodeDom.Compiler;
Alright...I am here looking for some bread crumbs. This is my code: using System;
Here is the my python code using BeautifulSoup. The main issue is with the
I'm using the code here and I want all the functions which call SDL
Having this code: using (BinaryWriter writer = new BinaryWriter(File.Open(ProjectPath, FileMode.Create))) { //save something here
Ok, here's what I'm trying to do... given this razor code @using(Html.WriteLater()) { output
Here's the code I'm using to make a custom AlertDialog. public void onButtonClicked(View v)

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.