0

As you can see below, I have Parallel.For loop. If run the program while file does not exist , it gives an error like "The process cannot access the file 'C:\ShowTime Error Logs\logFile.txt' because it is being used by another process."

However if file exist there is no error. I'm confused, my code already create the file even if file does not exist. Why it gives me an error if the file does not exist before run the program. And How can I fix it?

Thx

ErrorLog el = new ErrorLog();
        Parallel.For(0, 100000, delegate(int i)
    {
        el.WriteToShowTimeLog("logFile", "", "", (i).ToString(), "", "", @"C:\");
    });

ErrorLog class has a function and static variable

private static readonly object lock_ = new object();
    public  void WriteToShowTimeLog(string fileName, string errorMessage, string description, string movieID, string theaterID, string showTimeDate, string folderPath)
    {
        string filePath = folderPath + @"\ShowTime Error Logs\" + fileName + ".txt";
        lock (lock_)
        {

            if (!Directory.Exists(folderPath + @"\ShowTime Error Logs"))
            {
                Directory.CreateDirectory(folderPath + @"\ShowTime Error Logs");
            }

            if (!File.Exists(filePath))
            {
                File.Create(filePath);
            }

            using (StreamWriter dosya = new StreamWriter(filePath,true))
            {

                dosya.WriteLine("TheaterID:        " + theaterID);
                dosya.WriteLine("MovieID:          " + movieID);
                dosya.WriteLine("Show Time Date:   " + showTimeDate);
                dosya.WriteLine("Hata Mesajı:      " + errorMessage);
                dosya.WriteLine("Açıklama:         " + description);
                dosya.WriteLine("Hata Alınan Zaman:" + DateTime.Now);
                dosya.WriteLine("-------------------------------------------------------------------------------------------------------");
                dosya.Close();


            }
        }
    }
4

2 回答 2

2

You need to include the file checking and creation in the lock, otherwise you can create it from two separate thread simultaneously, producing the error you see.

EDIT

The File.Create returns an open file. Then the StreamWriter tries to open it again. You probably should simple set options for the streamwriter to use rather than trying to create it and then re-create it for the StreamWriter.

于 2012-04-07T16:51:26.970 回答
1

Anything outside the lock can be run in any order by any thread. Don't make assumptions to the contrary. In this case, you might have thread #1 check if the file exists. Since it doesn't, it enters the if clause and thread #2 takes over. Thread #2 checks if the file exists. Since it doesn't, also thread #2 enters the if clause and creates the file. Once thread #1 takes over again, surprise! File has already been created even if it had "just" checked that it didn't exist.

Most output streams will automatically create the file that's having data poured in, so it might not even be necessary to have that section.

于 2012-04-07T16:55:01.450 回答