| | |
Wait while file write in C#
Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
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
Views: 2376 | Replies: 8
| Thread Tools | Search this Thread |
Tag cloud for C#
.net access ado.net algorithm array bitmap box broadcast buttons c# chat check checkbox class client code color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development draganddrop drawing encryption enum event excel file files form format forms function gdi+ http httpwebrequest image index input install java label list listbox listener login mandelbrot math mouseclick mysql object operator oracle path photoshop picturebox pixelinversion post prime programming radians regex remote remoting resource richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update validation visualstudio webbrowser windows winforms wpf xml






