Can you lock one application variable in .Net?

In a .NET web service, you can set application level variables like

application("Stackoverflow") = "Stack"
application("Serverfault") = "Server"

      

If you get multiple requests, you can simply block, then update, and then unblock

try
  application.lock()
  application("Serverfault") = "Fault"
finally
  application.unlock()
end try

      

Now this locks all application variables, is there a way to just lock one application variable? Instead of all application variables at once?

+3


source to share


2 answers


See the documentation for Application.Lock .

The blocking method blocks other clients from changing the variables stored in the Application object, ensuring that only one client at a time can change or access the application variables.



It looks like the answer is no. If you don't provide your own locking strategy and use Application.Lock()

, and even then I'm not sure how the state is stored in Application

, if all the state is serialized on change, then you definitely won't be able to do that.

0


source


Interestingly, I had another problem I was looking into, which I think gives me a solution to the question I posted.

It:

  Using mutex = New Mutex(False, "LockServerFault")
      mutex.WaitOne()
      application("ServerFault") = "Fault"                                      
      mutex.ReleaseMutex()
  End Using

      



Performs the same as:

try
  application.lock()
  application("Serverfault") = "Fault"
finally
  application.unlock()
end try

      

But it does not lock all application variables, but it prevents one application variable from being updated at the same time. This allows me to update various application variables on different threads without making all threads pause for application variables that do not belong to them.

0


source







All Articles