If you want to write a file, but can potentially have several authors at once, you need to block

In asp.net web application, I want to write a file. This function will first receive data from the database, and then write a flat file.

What can I do to make only 1 record, and as soon as the record occurs, other streams that might want to write to the file do not happen from the moment of recording.

I want this recording to be done ONLY if it wasn’t done after 15 minutes.

I know there is a lock keyword, so I have to wrap everything in a lock and then check if it is updated in 15 minutes or more, or vice versa?

Update

Workflow:

Since this is a web application, multiple instances will be people viewing a particular web page. I could use the assembly in the cache system, but if asp.net processes it, it will be expensive to rebuild the cache, so I just want to write it to a flat file. My other option would be to simply create a Windows service, but this is the big management work I want.

+6
c # locking
source share
7 answers

Synchronize your write code to lock the shared object so that only one stream gets inside the block. Others wait until the current one comes out.

lock(this) { // perform the write. } 

Update: I assumed that you have a shared object. If these are different processes on the same machine, you need something like Named Mutex. Look at an example

+2
source share

Isn't it better to lock an object variable, rather than an entire instance?

+1
source share

The file I / O operations that are written to the file will automatically block the file. Check if the file is locked (by trying to write), and if it is not being written. Before making any entries, mark the timestamp in the file and see if it is longer than 15 minutes.

afaik, you cannot write to a file without blocking Windows / independently.

Now all that remains for you is to see how to do it using msdn (sorry, I can’t bother to look at all this, and I don’t remember C # classes very well). :)

+1
source share

I am not sure if .NET locking is applicable to various processes. In addition, lock (this) will exclude only other threads that start the method in the same instance of this, so other threads, even in the same process, can be launched immediately in different instances.

Assuming all your processes are running on the same computer, file locking should do this though.

If you work on different machines, your mileage may vary: win32 claims that file locking works on the network, but historically applications that rely on it (Think MSAccess) still have problems with file corruption.

0
source share
  // try enter will return false if another thread owns the lock if (Monitor.TryEnter(lockObj)) { try { // check last write time here, return if too soon; otherwise, write } finally { Monitor.Exit(lockobj); } } 
0
source share

Use file system locks

Like others, in this situation, .NET restrictions will be limited. Here is the code:

 FileInfo fi = new FileInfo(path); if (fi.Exists && (DateTime.UtcNow - fi.LastWriteTimeUtc < TimeSpan.FromMinutes(15)) { // file is fresh return; } FileStream fs; try { fs = new FileStream( path, FileMode.Create, FileAccess.Write, FileShare.Read); } catch (IOException) { // file is locked return; } using (fs) { // write to file } 

This will work through threads and processes.

0
source share

You guys should consider Mutex . It can be synchronized between multiple threads and processes.

0
source share

All Articles