Synchronization

It is important in many cases to synchronize access to application variables. In many cases, especially with things such as counters, you want only one thread to access an application variable and alter its contents at any given time. Suppose you have an application counter, and its accuracy is important. Now suppose that two different clients increment the variable at the exact same time. Will the counter be two more than original, or one? There’s no way to guarantee the behavior when simultaneous access happens.

Session variables don’t succumb to this same problem because there’s only one client access to any session variable at a time. The following code shows how to ensure that only a single thread can modify the contents ...

Get Special Edition Using® Microsoft® ASP.NET now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.