This question already has answers here:
How to ensure all data has been physically written to disk?
(7 answers)
Closed 6 years ago.
I am developing a windows service in C# which will likely write several times per second to a log. I am currently using System.IO.File.AppendAllText(time + message) for each operation and I am concerned so many writes may wear out the disk quickly.
Could this be a problem? Should I use a stream and flush every few seconds? Does the windows disk buffer protect against this?
Why you want to reinvent the wheel?
Better use some ready framework eg. Log4Net, NLog, SeriLog.
"Should I use a stream and flush every few seconds?"
What if your app crash with no flush stream? Log go away and you don't know what happens.
Related
This question already has answers here:
Is TCP 100% reliable? [closed]
(3 answers)
How reliable is a TCP connection?
(2 answers)
How reliable is .NET TCP protocol?
(5 answers)
Closed 5 years ago.
I'm currently working with the assumption that when sending TCP data with System.Net.Sockets.Socket.Send ,I am guaranteed to get an exception if the connection drops. Is it possible to have a connection drop at the OS layer without receiving a notification/exception in the C# application on top?
I don't imagine there is such a case, in .net, but how would I go about demonstrating this to someone that is skeptical.
Actually as far as i know It is possible. Imagine you create a packet send it and it gets dropped by the way. Os should automatically retransmit when ttl timesout. It will retry few times before giving up. More advanced firewalls have one small option as I remember. Drop with or without notification. Second looks like packet was 'lost' on the way to destination. They actually receive it but let's say- sends them to null without any answer.
I do not know how exactly socket.send works but from network point of view it is possible to not get confirmation for every packet that was lost/dropped.
This question already has answers here:
Should log file streams be opened/closed on each write or kept open during a desktop application's lifetime?
(13 answers)
Closed 6 years ago.
i need to write data into a CSV file each 600 msec using a c# application . The question: is better open and close file each time or keep it open until the end of write data actions? Note: i will change file name each day and each 60000 record
Thanck a lot for your opinions
CSV files are really easy to write to. If you don't know how to write to a file, the dotnetperls is your friend. You can simply call BinaryWriter.Write() to write anything. Write a value then a comma. That's it! If this file is going to be edited by the user at the time of running the application, then don't keep it open. Otherwise, keeping it open makes sure nothing unexpected happens.
This question already has answers here:
Notification when a file changes?
(3 answers)
Closed 8 years ago.
I have inherited application that, among many other things, has to watch if user writes/deletes text file into specific folder.
Currently, the application uses timer and polls after 5 seconds. I find this ineffective, and wish to improve this part of code.
My question is about existence of the .NET function that monitors changes in directory. Is there such function I can use to detect when a file is written/deleted in a specified folder?
Thank you.
Yes, you have the FileSystemWatcher class. It does exactly what you're looking for
Yes there is. I would suggest you take a look at the FileSystemWatcher class:
http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher%28v=vs.110%29.aspx
It's quite easy to set up, and it monitors for Win32 events, so is relatively inexpensive to use.
This question already has answers here:
How to get memory available or used in C#
(6 answers)
Closed 9 years ago.
I'm creating a server (Console App), but after doing some long-term testing I found out it grows eating RAM. For the local test suite, I am not working with much RAM
(8GB-DDR3 #2400MHz)Is there a way (In Program.cs, I assume) to restart the program if it is using over 'x' amount of RAM? Also, one way could be a timed loop/checkup?
You can use GC.GetTotalMemory. It returns an approximate value (long) of how much memory your program has allocated.
You can create a Timer object and make this comparison under the Tick event handler.
For more information, you can look here: http://msdn.microsoft.com/en-us/library/system.gc.gettotalmemory.aspx
I agree with what others have said about fixing your memory leak.
If you want to restart your program, create a second application that monitors the first process. Then, when memory gets too high in your original app, safely shut it down and allow the second application to launch it again.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is XMLDocument.Save an atomic operation?
I appear to have a Heisenbug in my program. I need to export an Xml file and then re-import it. The problem is that sometimes before the Xml file has finished saving the program will continue to the next line to try and re-import it and because it is not finished saving the application will crash. When I debug it there is enough time for the file to save so the program doesn't crash.
I could use Thread.Sleep but on slower computers it might still crash if it took too long.
I'm using XmlDocument.Save to save the file. I was wondering if there might be a way to implement a callback of some sort for when the file has been completely saved.
Suspect antivirus. Many virus scanners aren't written well and lock the file when you close it, preventing any program from opening it for a few seconds.
See this question:
Is it possible to reasonably workaround an antivirus scanning the working directory?