当我尝试使用线程重命名我的文件名并关闭并刷新 FileStream 时,它会给我错误无法访问关闭的文件



>我有大量数据,我需要每 5 秒制作一次".txt"文件,并将下一个数据写入新创建的".txt"文件中,但是当我尝试使用 Timer 线程时,程序运行正常,但一段时间后它将通过异常无法访问关闭的文件。请在我的代码中帮助我,并建议我必须做什么。

public class Program
{
public static void Main(string[] args)
{
Service s = new Service();
s.init();
}
}

class Service
{
FileStream fs = null;
int filecount = 0;
long a = 1000000000000000;
// int j = 0;
public void  init()
{
initialiseFile(filecount);
timer();
WriteINFile();
}
private void initialiseFile( int filecount)
{
fs = File.Create("C:\Users\yogesh.ghonate\source\repos\ConsoleApp3\ConsoleApp3\NewFolder1\Index_" + filecount + ".txt");        
}
private void WriteINFile()
{
string sen = " write in file ";
for (int i = 0; i < a; i++)
{
Byte[] title = new UTF8Encoding(true).GetBytes(sen);
fs.Write(title, 0, title.Length);
}
}
public void timer()
{
System.Timers.Timer th = new System.Timers.Timer();
th.Interval = 5000;
th.Elapsed += new ElapsedEventHandler(run);
th.Start();         
run(th, null);
}
public void run(object state, ElapsedEventArgs e)
{
Commitzfile();
}
private void Commitzfile()
{
//Stopwatch stopwatch = new Stopwatch();
//stopwatch.Start();
fs.Flush();
fs.Close();
// fs.Dispose();
//stopwatch.Stop();
filecount++;
initialiseFile(filecount);
}
}

最前沿的解决方案是将lock添加到代码中。这将防止任何争用条件,也会使你的代码变慢一些,因为它会在资源正在使用时阻塞线程。

你可以像这样使用它:

// add new lock object
object lockObject = new object();
FileStream fs = null;
int filecount = 0;
long a = 1000000000000000;
…
private void WriteINFile()
{
string sen = " write in file ";
for (int i = 0; i < a; i++)
{
Byte[] title = new UTF8Encoding(true).GetBytes(sen);
lock (lockObject)
{
fs.Write(title, 0, title.Length);
}
}
}
…
private void Commitzfile()
{
lock(lockObject)
{
fs.Flush();
fs.Close();
filecount++;
initialiseFile(filecount);
}
}

我建议您阅读以下有关锁定语句的文档


更新

在并发编程中,需要考虑许多问题。其中之一是资源管理。通常,这个问题是用餐饮哲学家问题来呈现的。当一个资源尝试由多个进程/线程使用,但资源一次必然由一个进程/线程使用时,就会发生主要问题。在您的情况下,它是FileStream,因为一个线程一直试图写入它,而在另一个线程上它关闭并更改。您可以看到,在某些情况下,可能会发生一个关闭流而另一个仍在尝试同时写入流的情况。若要解决此问题,需要确保一次只能由一个线程访问资源。执行此操作的一个工具是锁定语句。

其他要记住的事项:

  • 僵局。
  • 值得同时运行的并发线程数。

希望这个简短的总结有所帮助。如果您需要更多信息,请随时提出新问题(或查找是否已有问题)。

相关内容

最新更新