1

我写了一个简单的线程同步程序。但是当我运行这个程序时,我得到一个错误“进程无法访问文件'D:\Vivek.txt',因为它正被另一个进程使用。” 为什么我收到此错误。

class Program
{
    const string Filepath = "D:\\Vivek.txt";
    static AutoResetEvent writerwaithandle= new AutoResetEvent(true);// Signaled state
    static AutoResetEvent readerwaithandle = new AutoResetEvent(false);

    static void Main()
    {

        if (File.Exists(Filepath))
        {
            File.Delete(Filepath);                                          
        }

        File.CreateText(Filepath);
        CreateWriterThread();
        CreateReaderThread();
        Console.ReadKey();
    }

    private static void CreateWriterThread()
    {
        for (int i = 1; i <= 10;i++ )
        {

            var thread = new Thread(WriteFile);
            thread.Name = "Writer " + i;
            thread.Start();
            Thread.Sleep(250);
        }

    }

    private static void CreateReaderThread()
    {
        for (int i = 1; i <= 10; i++)
        {
            var thread = new Thread(ReadFile);
            thread.Name = "Reader " + i;
            thread.Start();
        }
    }

    private static void WriteFile()
    {
        writerwaithandle.WaitOne();

        var stream = new FileStream(Filepath, FileMode.Append);
        var streamwriter = new StreamWriter(stream);
        streamwriter.WriteLine("written by"+Thread.CurrentThread.Name+DateTime.Now));
        streamwriter.Flush();
        streamwriter.Close();

        readerwaithandle.Set();
    }

    private static void ReadFile()
    {
        readerwaithandle.WaitOne();
        if (File.Exists(Filepath))
        {
            var stream = new FileStream(Filepath, FileMode.Open);
            var streamreader = new StreamReader(stream);
            var text = streamreader.ReadToEnd();
            streamreader.Close();
            Console.WriteLine("Read by thread {0} \n",Thread.CurrentThread.Name);
            Console.WriteLine(text);
        }
        writerwaithandle.Set();
    }
}

当我替换代码时

if (File.Exists(Filepath))
{
  File.Delete(Filepath);                                          
}
  File.CreateText(Filepath);

if (!File.Exists(Filepath))
{
  File.CreateText(Filepath);
}

该程序第一次显示相同的错误。之后它永远不会给出任何错误。请任何人告诉我错误区域,原因以及最好的解决方案。

4

2 回答 2

2

当您使用时,请始终FileStream使用它using

using (var fileStream = new FileStream(Filepath, FileMode.Open))
{
   Your code...
}

这确保了流得到正确处理。

您还可以使用StreamReaderusing

using (var fileStream = new FileStream(Filepath, FileMode.Open))
{
    using (var reader = new StreamReader(stream))
    {
        Your code...
    }
}
于 2013-09-03T05:41:04.217 回答
1

保持警惕,查看文档File.CreateText

创建或打开用于写入 UTF-8 编码文本的文件。

返回值 类型:System.IO.StreamWriter 写入的 StreamWriter

使用 UTF-8 编码到指定文件

这意味着不需要创建新的 FileStream,因为 FileStream 已经创建并在您使用时返回File.CreateText。您应该只在代码中使用创建的 FileStream。

这是您的代码的固定版本:

 class Program
    {
        const string Filepath = "D:\\Vivek.txt";
        static AutoResetEvent writerwaithandle = new AutoResetEvent(true);// Signaled state
        static AutoResetEvent readerwaithandle = new AutoResetEvent(false);

        static void Main()
        {

            if (File.Exists(Filepath))
            {
                File.Delete(Filepath);
            }

            //File.CreateText(Filepath);
            CreateWriterThread();
            CreateReaderThread();
            Console.ReadKey();
        }

        private static void CreateWriterThread()
        {
            for (int i = 1; i <= 10; i++)
            {

                var thread = new Thread(WriteFile);
                thread.Name = "Writer " + i;
                thread.Start();
                Thread.Sleep(250);
            }

        }

        private static void CreateReaderThread()
        {
            for (int i = 1; i <= 10; i++)
            {
                var thread = new Thread(ReadFile);
                thread.Name = "Reader " + i;
                thread.Start();
            }
        }

        private static void WriteFile()
        {
            writerwaithandle.WaitOne();


            var streamwriter = File.CreateText(Filepath);

            //var stream = new FileStream(Filepath, FileMode.Append);
            //var streamwriter = new StreamWriter(stream);
            streamwriter.WriteLine("written by" + Thread.CurrentThread.Name + DateTime.Now);
            streamwriter.Flush();
            streamwriter.Close();

            readerwaithandle.Set();
        }

        private static void ReadFile()
        {
            readerwaithandle.WaitOne();
            if (File.Exists(Filepath))
            {
                var stream = new FileStream(Filepath, FileMode.Open);
                var streamreader = new StreamReader(stream);
                var text = streamreader.ReadToEnd();
                streamreader.Close();
                Console.WriteLine("Read by thread {0} \n", Thread.CurrentThread.Name);
                Console.WriteLine(text);
            }
            writerwaithandle.Set();
        }
    }
于 2013-09-03T06:02:29.823 回答