Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.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# - Fatal编程技术网

C# 另一进程正在使用的文件。原因和解决方案?

C# 另一进程正在使用的文件。原因和解决方案?,c#,C#,进程无法访问该文件 “abc.log”,因为它正被 另一个过程 您好,我偶尔在我的应用程序中看到这个异常,但我仍在尝试修复它,希望我能从stackoverflow中获得一些见解 在我的应用程序中,我定义了一个WriteToLog函数,它将写入日志文件。 另外,我的应用程序的主窗体将启动backgroundWorker do some job,它也调用WriteLog。可能两个线程访问一个文件会导致问题?但在我的代码中 我已经尽了最大努力编写flush并关闭文本文件(我想),下面是我在WriteT

进程无法访问该文件 “abc.log”,因为它正被 另一个过程

您好,我偶尔在我的应用程序中看到这个异常,但我仍在尝试修复它,希望我能从stackoverflow中获得一些见解

在我的应用程序中,我定义了一个WriteToLog函数,它将写入日志文件。 另外,我的应用程序的主窗体将启动backgroundWorker do some job,它也调用WriteLog。可能两个线程访问一个文件会导致问题?但在我的代码中 我已经尽了最大努力编写flush并关闭文本文件(我想),下面是我在WriteToLog中的代码:

 StreamWriter sw = null;
 string newText = "";
 try
 {
     //populate the content for newText
     sw = File.AppendText(LOG_FILE);
     sw.Write(newText);
     sw.Flush();
     sw.Close();
 }
 catch (IOException ex)
 {
     MessageBox.Show("Failed to write to log!\n\t" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
 }
 finally
 {
     if (sw != null)
     {
         sw.Close();
     }
 }

我认为只要我刷新并关闭streamWriter,我就应该能够在多线程中多次调用WriteToLog,不是吗?或者,如果我错了,我可以简单地将文件打开共享,或者有其他原因/解决方案吗?

如果您是从多个线程调用此函数,则必须确保文件操作是原子的。线程A很可能会超过AppendText并停止(例如,在写入时),此时线程B将被调度并开始写入相同(现在已锁定)的文件


处理此问题的一种方法是在代码示例中的语句周围放置一个区域。

否;试图同时打开文件的不同线程将相互干扰。您可以
锁定
共享的
对象
,强制线程一次运行一个


或者只使用内置的;您可以将
app.config
配置为写入文件。

这可能与您的问题无关,但是您可以通过在
StreamWriter
中使用
语句来简化此代码,而不是使用
finally
。这还有一个额外的好处,即不必显式调用
Close
Flush
(关闭流也会刷新它)

我还没有尝试编译这个,但应该是这样的:

string newText = "";
try
{
    //populate the content for newText
    using (StreamWriter sw = File.AppendText(LOG_FILE))
    {
        sw.Write(newText);
    }
}
catch (IOException ex)
{
    MessageBox.Show("Failed to write to log!\n\t" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

谢谢你,我以前遇到过使用,但没有费心修改我的代码。但请再想一想,即使它不应该改变我的应用程序的工作方式,我也将代码改为使用block,只是为了更清晰的代码。您好,如果我的理解是正确的,跟踪功能是.NET的调试工具,我可以用它来编写日志文件?我需要重新测试应用程序,显然,只需锁定日志文件或调用extWriter.Synchronized(FILE.AppendText(filePath));而不是sw=File。AppendText(日志文件)没有解决问题。@pstar:跟踪用于调试和跟踪。默认情况下,对于发布版本,启用跟踪并禁用调试。