In many cases, it is important to synchronize access to application variables. 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 exactly the same time. Will the counter be two more than the original, or just one? No way exists to guarantee the behavior once simultaneous access happens.
Session variables do not succumb to the same problem because there is only one client access at a time to any session variable. 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"] = (int)Application["SomeKey"] + 1; Application.Unlock();VB
Application.Lock() Application("SomeKey") = Application("SomeKey") + 1 Application.Unlock()