Windows Form Timer and New Thread problem accessing file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stoogots2
    New Member
    • Sep 2007
    • 77

    Windows Form Timer and New Thread problem accessing file

    I have a Windows Form app in which a Windows Timer (when it ticks) starts a new thread to do a long running process.

    Code:
                                
                                ThreadStart ts = new ThreadStart(Task.RunTask);
                                Thread thr = new Thread(ts);
                                thr.Start();
    During the execution of this thread, I am getting an error writing to a Logfile (also using a static class).

    Code:
                    tw1 = TextWriter.Synchronized(File.AppendText(sLogFileName));
                        tw1.WriteLine(DataToWrite);
                        tw1.Flush();
    Anyway, I guess the problem is that multiple threads are attempting to write to the same log file, but after perusing online (and making numerous changes) , I am still getting the error (cannot access file as it is already in use).

    Any help would be appreciated.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    You don't close the file when you're done with it in that code

    Comment

    • stoogots2
      New Member
      • Sep 2007
      • 77

      #3
      Part of the code is in a try catch block, and the finally statement contains the .close() method. I don't understand how it is being locked, though. I thought it might be the fact that it is a different thread (since I am less experienced in that than in file IO).

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Could be that when you open the file with AppendText it locks the file for other writes.

        You should probably only open the file once, then use that syncronized textwriter in your threads

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          My suggestion is to not have different bits of code trying to write to a single file.
          Instead have a single class that does your logging, and raise an event with the text to be logged. When your logging class 'hears' the event it will take the string (argument) and write it.

          This way you can include a log entry just about anyplace with a single line of

          RaiseLog("Error code 145");

          Comment

          • PRR
            Recognized Expert Contributor
            • Dec 2007
            • 750

            #6
            In case you are using system.windows. timer then i guess does not create threads for executing timer function... The UI thread does the execution... As you are creating separate threads in your timer function.. you could use System.Timers.T imer... This creates a separate thread.....
            As for your locking the file ... you could use a static object that you could lock on entering the timer function. Look into Monitor.TryEnte r Method

            Comment

            Working...