Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you lock a single application variable in .Net?

In .Net web service you can set application level variables like this

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

If you receive multiple requests, you can simply lock, then update, then unlock

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

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

like image 537
Henry Avatar asked Jan 26 '26 05:01

Henry


1 Answers

Interestingly enough, I had a different issue I was researching, which I think provides me a solution the question I posted.

This:

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

Accomplishes the same thing as:

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

But it doesn't lock all application variables, but it does prevent the single application variable from being updated at the same time. This enables me to update different application variables in different threads, without making all threads pause for application variables that don't apply to them.

like image 88
Henry Avatar answered Jan 27 '26 19:01

Henry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!