Wait while file write in C#

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2008
Posts: 66
Reputation: ambarisha.kn is an unknown quantity at this point 
Solved Threads: 0
ambarisha.kn ambarisha.kn is offline Offline
Junior Poster in Training

Wait while file write in C#

 
0
  #1
Jan 21st, 2009
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.
Ambarish
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,039
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Wait while file write in C#

 
0
  #2
Jan 22nd, 2009
What logging framework are you using that's giving you these problems?
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 66
Reputation: ambarisha.kn is an unknown quantity at this point 
Solved Threads: 0
ambarisha.kn ambarisha.kn is offline Offline
Junior Poster in Training

Re: Wait while file write in C#

 
0
  #3
Jan 22nd, 2009
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.
  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. }
Ambarish
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,039
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Wait while file write in C#

 
0
  #4
Jan 22nd, 2009
Use log4net or NLog or some other framework for your logging.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 66
Reputation: ambarisha.kn is an unknown quantity at this point 
Solved Threads: 0
ambarisha.kn ambarisha.kn is offline Offline
Junior Poster in Training

Re: Wait while file write in C#

 
0
  #5
Jan 22nd, 2009
dont know how to use. tell me how?
Last edited by ambarisha.kn; Jan 22nd, 2009 at 3:01 am.
Ambarish
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 44
Reputation: hieuuk is an unknown quantity at this point 
Solved Threads: 4
hieuuk hieuuk is offline Offline
Light Poster

Re: Wait while file write in C#

 
0
  #6
Jan 22nd, 2009
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);
}
.Net Developer - 3D Game Designer
My Portfolio/Blog: http://www.hieu.co.uk
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 135
Reputation: cVz is an unknown quantity at this point 
Solved Threads: 7
cVz's Avatar
cVz cVz is offline Offline
Junior Poster

Re: Wait while file write in C#

 
0
  #7
Jan 23rd, 2009
Just make sure you close the stream...

Else , like hieuuk said,

do the
Thread.sleep(1000) // 1 second
Delphi & C# programmer deluxe...
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: Wait while file write in C#

 
0
  #8
Jan 23rd, 2009
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.
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 21
Reputation: nelis is an unknown quantity at this point 
Solved Threads: 7
nelis nelis is offline Offline
Newbie Poster

Re: Wait while file write in C#

 
0
  #9
Jan 23rd, 2009
Originally Posted by LizR View Post
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC