| | |
Wait while file write in C#
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2008
Posts: 66
Reputation:
Solved Threads: 0
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.
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)
public void WriteLog(LogType type, LogStatus status, string message, params object[] args) { DateTime now = DateTime.Now; string path = GetPath(type, now); string appname; string usrname; if (AppService.Provider.CurrentAppType == null) appname = "-"; else appname = AppService.Provider.CurrentAppType.ToString(); if (UserService.Provider.CurrentUser == null) usrname = "-"; else usrname = UserService.Provider.CurrentUser.Name; appname = (appname + new string(' ', 10)).Substring(0, 10); usrname = (usrname + new string(' ', 16)).Substring(0, 16); string line = now.ToString("HH:mm:ss.fff", CultureInfo.InvariantCulture) + " " + status.ToString().Substring(0, 1) + " " + appname + " " + usrname + " " + string.Format(CultureInfo.InvariantCulture, message, args) + Environment.NewLine; lock (_lck) { File.AppendAllText(path, line); #if DEBUG Console.Write(line); #endif } }
Ambarish
•
•
Join Date: Nov 2008
Posts: 44
Reputation:
Solved Threads: 4
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);
}
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
My Portfolio/Blog: http://www.hieu.co.uk
•
•
Join Date: Aug 2008
Posts: 1,735
Reputation:
Solved Threads: 186
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.
•
•
Join Date: Jan 2009
Posts: 21
Reputation:
Solved Threads: 7
•
•
•
•
so as other have said open it, dont close it until you're done with it
-Nelis
![]() |
Similar Threads
- find duplicates from a spreadsheet file (Computer Science)
- Search a particular file (Visual Basic 4 / 5 / 6)
- remove extra blank spaces in a text file (C++)
- Image read and write operations in turbo C (C)
- how to create a zig zag encryption (C++)
- Is it possible to arange lines in a file alphabetically? (C)
- read text file (C)
- Execute Multiple Things At One Time (Visual Basic 4 / 5 / 6)
- Moving avatars (C++)
Other Threads in the C# Forum
- Previous Thread: forcing set list in parameter list prompts?
- Next Thread: Inserting Empty Line in String Variable
| Thread Tools | Search this Thread |
.net access algorithm array asp barchart bitmap box broadcast buttons c# check checkbox client column combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development display draganddrop drawing encryption enum equation event excel file form format formbox forms formupdate function gdi+ httpwebrequest image index input install java label linux list listbox mandelbrot math mouseclick mysql networking operator packaging parse path photoshop picturebox pixelinversion post powerpacks programming radians regex remote remoting reporting richtextbox robot server sleep socket sql statistics stream string table text textbox thread time timer transform treeview update usercontrol validation visualstudio webbrowser wfa windows winforms wpf xml






