View Single Post
  #9 (permalink)  
Old August 22nd, 2004, 08:20 AM
Imar's Avatar
Imar Imar is offline
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts

BeginWrite and BeginRead are used for asynchronous writing and reading within a method / application. So your main thread can continue working, while a separate thread takes care of IO stuff.

In the hitcounter scenario this is not going to help you at all. You still have the issue of two users writing at the same time, each from within a separate thread. Maybe the problem can even get worse, when the second user writes a different value to the file than the first.
Conclusion: you cannot use this file based method on a busy site; you'll always run into problem when two users try to write to the file at the same time.
I understand you're trying to learn working with files, but you picked the wrong topic to try it out with.

Theoretically, you could also use Application state for the values. However, I chose static variables as it is a concept that easily translates to other (.NET) applications.

Using application state is no problem, even with 100 or 1000 or 100,000 users for the hit counter. All you are saving is the value of the hitcounter, and the number of users you have queued. So, all you'd be saving in Application state are two ints. That shouldn't affect performance at all.

Does this clarify things?

Imar Spaanjaars
Everyone is unique, except for me.