c# 多线程按顺序写 大文件 代码
以下是一个简单的示例,演示如何使用多线程按顺序读取和写入大文件:
using System;
using System.IO;
using System.Threading;
class Program
{
static void Main(string[] args)
{
string sourceFile = "input.txt";
string destFile = "output.txt";
int bufferSize = 1024 * 1024; // 1MB buffer size
int threadCount = 4; // number of threads to use
// create a semaphore to limit the number of threads accessing the file at once
SemaphoreSlim semaphore = new SemaphoreSlim(threadCount);
// open the source and destination files
using (FileStream sourceStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize))
using (FileStream destStream = new FileStream(destFile, FileMode.Create, FileAccess.Write, FileShare.Write, bufferSize))
{
// calculate the number of chunks to read and write
long fileSize = sourceStream.Length;
int chunkCount = (int)Math.Ceiling((double)fileSize / bufferSize);
// create an array of manual reset events to signal when each chunk has been written
ManualResetEvent[] events = new ManualResetEvent[chunkCount];
// loop through each chunk and start a new thread to read and write it
for (int i = 0; i < chunkCount; i++)
{
// create a new event for this chunk
events[i] = new ManualResetEvent(false);
// wait for a semaphore slot to become available
semaphore.Wait();
// start a new thread to read and write this chunk
ThreadPool.QueueUserWorkItem(state =>
{
int index = (int)state;
byte[] buffer = new byte[bufferSize];
int bytesRead = 0;
try
{
// read the chunk from the source file
sourceStream.Seek(index * bufferSize, SeekOrigin.Begin);
bytesRead = sourceStream.Read(buffer, 0, bufferSize);
// write the chunk to the destination file
destStream.Seek(index * bufferSize, SeekOrigin.Begin);
destStream.Write(buffer, 0, bytesRead);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
// release the semaphore slot and signal the event
semaphore.Release();
events[index].Set();
}
}, i);
}
// wait for all events to be signaled
WaitHandle.WaitAll(events);
}
}
}
此代码将输入文件分成多个块,并使用多个线程并行读取和写入每个块。使用信号量来限制并发访问文件的数量,以避免资源争用。最后,使用 WaitHandle.WaitAll 等待所有块都已写入目标文件。
原文地址: https://www.cveoy.top/t/topic/bIdw 著作权归作者所有。请勿转载和采集!