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 of an application variable. The code calls the Lock() method, alters the variable, and then calls the Unlock() method.

 ' C#  Application.Lock();  Application["SomeKey"]++;  Application.Unlock();  ' VB  Application.Lock()  Application("SomeKey") = Application("SomeKey") + 1  Application.Unlock() 

Special Edition Using ASP. NET
Special Edition Using ASP.Net
ISBN: 0789725606
EAN: 2147483647
Year: 2002
Pages: 233 © 2008-2017.
If you may any questions please contact us: