943,670 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 6244
  • C# RSS
Jan 21st, 2009
0

Wait while file write in C#

Expand Post »
How to wait for some time while writing to a file.

I am appending some text in log file, its giving error as Some other process using this file.

How i have to wait untill its free.
Similar Threads
Reputation Points: 25
Solved Threads: 0
Junior Poster in Training
ambarisha.kn is offline Offline
66 posts
since Jun 2008
Jan 22nd, 2009
2

Re: Wait while file write in C#

What logging framework are you using that's giving you these problems?
Team Colleague
Reputation Points: 1133
Solved Threads: 171
Super Senior Demiposter
Rashakil Fol is offline Offline
2,478 posts
since Jun 2005
Jan 22nd, 2009
0

Re: Wait while file write in C#

Here, how i am writing log to a file.
Its giving error only for some time. It may or may not give error as "Some other process using this file".

How to rectify this. Is it possible to wait untill other process release this file.
C# Syntax (Toggle Plain Text)
  1. public void WriteLog(LogType type, LogStatus status, string message, params object[] args)
  2. {
  3. DateTime now = DateTime.Now;
  4.  
  5. string path = GetPath(type, now);
  6.  
  7. string appname;
  8. string usrname;
  9.  
  10. if (AppService.Provider.CurrentAppType == null)
  11. appname = "-";
  12. else
  13. appname = AppService.Provider.CurrentAppType.ToString();
  14.  
  15. if (UserService.Provider.CurrentUser == null)
  16. usrname = "-";
  17. else
  18. usrname = UserService.Provider.CurrentUser.Name;
  19.  
  20. appname = (appname + new string(' ', 10)).Substring(0, 10);
  21. usrname = (usrname + new string(' ', 16)).Substring(0, 16);
  22.  
  23. string line = now.ToString("HH:mm:ss.fff", CultureInfo.InvariantCulture) + " " +
  24. status.ToString().Substring(0, 1) + " " +
  25. appname + " " +
  26. usrname + " " +
  27. string.Format(CultureInfo.InvariantCulture, message, args) + Environment.NewLine;
  28.  
  29. lock (_lck)
  30. {
  31. File.AppendAllText(path, line);
  32. #if DEBUG
  33. Console.Write(line);
  34. #endif
  35. }
  36. }
Reputation Points: 25
Solved Threads: 0
Junior Poster in Training
ambarisha.kn is offline Offline
66 posts
since Jun 2008
Jan 22nd, 2009
2

Re: Wait while file write in C#

Use log4net or NLog or some other framework for your logging.
Team Colleague
Reputation Points: 1133
Solved Threads: 171
Super Senior Demiposter
Rashakil Fol is offline Offline
2,478 posts
since Jun 2005
Jan 22nd, 2009
0

Re: Wait while file write in C#

dont know how to use. tell me how?
Last edited by ambarisha.kn; Jan 22nd, 2009 at 3:01 am.
Reputation Points: 25
Solved Threads: 0
Junior Poster in Training
ambarisha.kn is offline Offline
66 posts
since Jun 2008
Jan 22nd, 2009
0

Re: Wait while file write in C#

Using a while loop arround the code open and write to the file.
Suggestion, put that code into another thread, so if it can't write out yet. Just sleep for one sec and try again. Something like this could do a dirty trick;
writtenOut = false;
while writtenOut=false
{
open file
if could open file
write to file
close file;
writtenOut=true;
sleep(1000);
}
Reputation Points: 11
Solved Threads: 4
Light Poster
hieuuk is offline Offline
44 posts
since Nov 2008
Jan 23rd, 2009
0

Re: Wait while file write in C#

Just make sure you close the stream...

Else , like hieuuk said,

do the
Quote ...
Thread.sleep(1000) // 1 second
cVz
Reputation Points: 29
Solved Threads: 7
Junior Poster
cVz is offline Offline
139 posts
since Mar 2008
Jan 23rd, 2009
0

Re: Wait while file write in C#

You should only get the some other process is using that file if you're trying to open it, so as other have said open it, dont close it until you're done with it. Then there is no major delay to writing the file.
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Jan 23rd, 2009
0

Re: Wait while file write in C#

Click to Expand / Collapse  Quote originally posted by LizR ...
so as other have said open it, dont close it until you're done with it
I'd not recommend this in practice. If you open it and just hold on to it, what other process is now failing because you're holding onto resources? How about doing some research to find out what other process is using the file? If it's your own code doing it, then there's probably a bug in there somewhere of not releasing it properly that needs to be corrected. I'd look at something like HandleEx from sysinternals to determine what is holding onto the file.

-Nelis
Reputation Points: 15
Solved Threads: 7
Newbie Poster
nelis is offline Offline
21 posts
since Jan 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: forcing set list in parameter list prompts?
Next Thread in C# Forum Timeline: Inserting Empty Line in String Variable





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC