By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,928 Members | 1,210 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,928 IT Pros & Developers. It's quick & easy.

CC# "Lock" equivalent in VB.NET?

P: n/a
Hi

I am converting some code from C# to VB.NET and I have come across a command
that I can't find the VB equivalent. The C# command is 'lock' and I think
it is used to lock a data type from being used by other threads until it is
released. (?)

Here is the example:

lock(typeof(className))
{
if SkyIsBlue = True
{
//** Do somestuff in here
}
else
{
//** Run For Cover
//** Do somestuff in here
}
}

I tried to use lock, but it was only for locking files for reading/writing
and not the same. Is there an equivalent command in VB.NET or a way to
replicate it?

Thanks!
--Scott
Nov 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
The C# lock statement is equivelent to calling Monitor.Enter and
Monitor.Exit. So you can use those methods in VB.

"Scott Johnson" <Un*****@User.com> wrote in message
news:10*************@corp.supernews.com...
Hi

I am converting some code from C# to VB.NET and I have come across a command that I can't find the VB equivalent. The C# command is 'lock' and I think
it is used to lock a data type from being used by other threads until it is released. (?)

Here is the example:

lock(typeof(className))
{
if SkyIsBlue = True
{
//** Do somestuff in here
}
else
{
//** Run For Cover
//** Do somestuff in here
}
}

I tried to use lock, but it was only for locking files for reading/writing
and not the same. Is there an equivalent command in VB.NET or a way to
replicate it?

Thanks!
--Scott

Nov 20 '05 #2

P: n/a
* "Scott Johnson" <Un*****@User.com> scripsit:
I am converting some code from C# to VB.NET and I have come across a command
that I can't find the VB equivalent. The C# command is 'lock' and I think
it is used to lock a data type from being used by other threads until it is
released. (?)


'SyncLock'.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #3

P: n/a
Marina,
Actually C# lock is equivalent to the VB.NET SyncLock statement as Herfried
indicated.

You are correct in that both implemented in terms of Monitor.Enter, however
they also both use a Try/Finally to be certain that the Exit is called!

I would recommend using SyncLock instead of Monitor.Enter directly, unless I
specifically needed Monitor.Enter.

Hope this helps
Jay

"Marina" <so*****@nospam.com> wrote in message
news:u6**************@TK2MSFTNGP11.phx.gbl...
The C# lock statement is equivelent to calling Monitor.Enter and
Monitor.Exit. So you can use those methods in VB.

"Scott Johnson" <Un*****@User.com> wrote in message
news:10*************@corp.supernews.com...
Hi

I am converting some code from C# to VB.NET and I have come across a

command
that I can't find the VB equivalent. The C# command is 'lock' and I think it is used to lock a data type from being used by other threads until it

is
released. (?)

Here is the example:

lock(typeof(className))
{
if SkyIsBlue = True
{
//** Do somestuff in here
}
else
{
//** Run For Cover
//** Do somestuff in here
}
}

I tried to use lock, but it was only for locking files for reading/writing and not the same. Is there an equivalent command in VB.NET or a way to
replicate it?

Thanks!
--Scott


Nov 20 '05 #4

P: n/a
Thanks to you all for your help. I appreciate it.

--Scott

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:O8*************@TK2MSFTNGP12.phx.gbl...
Marina,
Actually C# lock is equivalent to the VB.NET SyncLock statement as Herfried indicated.

You are correct in that both implemented in terms of Monitor.Enter, however they also both use a Try/Finally to be certain that the Exit is called!

I would recommend using SyncLock instead of Monitor.Enter directly, unless I specifically needed Monitor.Enter.

Hope this helps
Jay

"Marina" <so*****@nospam.com> wrote in message
news:u6**************@TK2MSFTNGP11.phx.gbl...
The C# lock statement is equivelent to calling Monitor.Enter and
Monitor.Exit. So you can use those methods in VB.

"Scott Johnson" <Un*****@User.com> wrote in message
news:10*************@corp.supernews.com...
Hi

I am converting some code from C# to VB.NET and I have come across a

command
that I can't find the VB equivalent. The C# command is 'lock' and I think it is used to lock a data type from being used by other threads until it
is
released. (?)

Here is the example:

lock(typeof(className))
{
if SkyIsBlue = True
{
//** Do somestuff in here
}
else
{
//** Run For Cover
//** Do somestuff in here
}
}

I tried to use lock, but it was only for locking files for reading/writing and not the same. Is there an equivalent command in VB.NET or a way

to replicate it?

Thanks!
--Scott



Nov 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.