How to do this C# code in VB?

G

G Dean Blake

This is some C# code out of a book that is part of a server component using
Application state. I'm trying to convert it to VB. I can't find the VB
equivilent of the Lock. I thought it might be a Mutex but those docs look
different. What is the VB equivelent of the Lock code below?
Thanks,
G

protected override void OnLoad(EventArgs e) {
base.OnLoad(e);
switch (TrackingMode){
case PageTrackingMode.ByApplication:
// Update the page hits in the Application
// object. This operation needs a lock because
// multiple users could access a page at the same
// time.
lock(Page.GetType()){
Page.Application[HitsKey] = Hits + 1;
}
break;
 
S

Scott Allen

There is a SyncLock statement in VB you can use.

To be honest, the code isn't that great. If the code used a Shared
variable (static in C#) somewhere, it could increment the hit count
with System.Threading.Interlocked.Increment(ref Hits); This would
avoid the Application (which will require boxing and unboxing of the
int values (assuming Hits is an int)), and avoid the lock altogether.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top