<p>In a asp.net web application, I want to write to a file.  This function will first get data from the database, and then write out the flat file.</p>

<p>What can I do to make sure only 1 write occurs, and once the write occurrs, the other threads that maybe want to write to the file don't since the write took place.</p>

<p>I want to have this write done ONLY if it hasn't been done in say 15 minutes.</p>

<p>I know there is a lock keyword, so should I wrap everything in a lock, then check if it has been updated in 15 minutes or more, or visa versa?</p>

<p><b>Update</b></p>

<p>Workflow:</p>

<p>Since this is a web application, the multiple instances will be people viewing a particular web page.  I could use the build in cache system, but if asp.net recycles it will be expensive to rebuild the cache so I just want to write it out to a flat file.  My other option would be just to create a windows service, but that is more work to manage that I want.</p>

Synchronize your writing code to lock on a shared object so that only one thread gets inside the block. Others wait till the current one exits.
```
```
lock(this)
{
  // perform the write.
}
```
```
Update: I assumed that you have a shared object. If these are different processes on the same machine, you'd need something like a Named Mutex.
Looky here for an example