Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将数据写入文件的最有效方法是什么?_C#_Winapi_File Io - Fatal编程技术网

C# 将数据写入文件的最有效方法是什么?

C# 将数据写入文件的最有效方法是什么?,c#,winapi,file-io,C#,Winapi,File Io,我需要在我的应用程序中写入大量数据。数据定期到达并被推入队列 //producer queue_.push( new_data );// new_data is a 64kb memory range PostThreadMessage(worker_thread_id_, MyMsg, 0, 0);//notify the worker thread 之后,我需要将这些数据写入文件。我有另一个线程,它从队列中获取数据并将其写入 //consumer while(GetMessage(msg,

我需要在我的应用程序中写入大量数据。数据定期到达并被推入队列

//producer
queue_.push( new_data );// new_data is a 64kb memory range
PostThreadMessage(worker_thread_id_, MyMsg, 0, 0);//notify the worker thread
之后,我需要将这些数据写入文件。我有另一个线程,它从队列中获取数据并将其写入

//consumer
while(GetMessage(msg, 0, 0))
{
   data = queue_.pop();
   WriteFile(file_handle_, data.begin(), data.size(), &io_bytes, NULL);
}
这很简单,但效率不高。 另一个机会是使用IO完成端口

//producer
//file must be associated with io completion port
WriteFile( file_handle_
         , new_data.begin()
         , new_data.size()
         , &io_bytes
         , new my_overlaped(new_data) );
消费者只需删除未使用的缓冲区

my_completion_key* ck = 0;
my_overlapped* op = 0;
DWORD res = GetQueuedIOCompletionStatus(completion_port_, &io_bytes, &ck, &op);
//process errors
delete op->data;

在windows上执行大量文件i/o操作的首选方法是什么?

很难说,因为目前的方法不清楚您遇到了什么样的问题。在专用“编写器”线程中异步不应导致性能的显著提高

如果您有少量但频繁的写入(因此频繁写入文件的成本成为瓶颈),我建议实现一种延迟写入—收集数据,并在达到某个卷阈值时将其刷新到文件中