473,659 Members | 2,591 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

CC# "Lock" equivalent in VB.NET?

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(cla ssName))
{
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
4 12343
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.c om> wrote in message
news:10******** *****@corp.supe rnews.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(cla ssName))
{
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
* "Scott Johnson" <Un*****@User.c om> 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
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******** ******@TK2MSFTN GP11.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.c om> wrote in message
news:10******** *****@corp.supe rnews.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(cla ssName))
{
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
Thanks to you all for your help. I appreciate it.

--Scott

"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:O8******** *****@TK2MSFTNG P12.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******** ******@TK2MSFTN GP11.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.c om> wrote in message
news:10******** *****@corp.supe rnews.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(cla ssName))
{
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2462
by: Aaron Davies | last post by:
I'm developing a collaborative whiteboard, in which all objects (shapes, clip art icons, etc.) are synchronized between all participants in a session. It's working well, but I'm running into a problem: if two people try to drag the same object at the same time, nothing prevents them from doing so, and whichever one them lets go first will have his move be the one that takes effect. (The other person's client will probably crash at the...
1
2385
by: Ron M. Newman | last post by:
Hi. Quick question: - I have a class with fields. There is one method that modifies them. in this method I have something like lock (this.lockObject) { /// throwing exception here... }
94
30272
by: Samuel R. Neff | last post by:
When is it appropriate to use "volatile" keyword? The docs simply state: " The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock Statement (C# Reference) statement to serialize access. " But when is it better to use "volatile" instead of "lock" ?
6
1910
by: Curious | last post by:
I have an arraylist used in three separate methods. In method #1 (event method), some items are removed from the arraylist if certain conditions are met; In method #2 (event method), properties of some items are modified if certain conditions are met; In method #3, it loops through each item in the arraylist without changing any item on the arraylist or removing any item from the
0
8428
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8339
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8629
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7360
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4176
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1982
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1739
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.