PC Review


Reply
Thread Tools Rate Thread

CC# "Lock" equivalent in VB.NET?

 
 
Scott Johnson
Guest
Posts: n/a
 
      16th Jul 2004
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


 
Reply With Quote
 
 
 
 
Marina
Guest
Posts: n/a
 
      16th Jul 2004
The C# lock statement is equivelent to calling Monitor.Enter and
Monitor.Exit. So you can use those methods in VB.

"Scott Johnson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      16th Jul 2004
* "Scott Johnson" <(E-Mail Removed)> 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/>
 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      16th Jul 2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> The C# lock statement is equivelent to calling Monitor.Enter and
> Monitor.Exit. So you can use those methods in VB.
>
> "Scott Johnson" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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
> >
> >

>
>



 
Reply With Quote
 
Scott Johnson
Guest
Posts: n/a
 
      16th Jul 2004
Thanks to you all for your help. I appreciate it.

--Scott



"Jay B. Harlow [MVP - Outlook]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > The C# lock statement is equivelent to calling Monitor.Enter and
> > Monitor.Exit. So you can use those methods in VB.
> >
> > "Scott Johnson" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> > > 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
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
VB.NET equivalent to "classic" VB "DoEvents" =?Utf-8?B?cGNuZXJk?= Microsoft VB .NET 1 13th Jun 2006 04:36 AM
Feature to "Lock the View" similar to "Lock the Taskbar" =?Utf-8?B?QnJhZDI0MA==?= Microsoft Outlook Discussion 0 7th Feb 2006 05:08 PM
Defragmentation created "freeze" or "lock-up" problems =?Utf-8?B?R3JhbnQgTm9lc2tl?= Windows XP General 1 25th Mar 2005 10:55 PM
Is there anyway to LOCK "View" to open in "Details" I do not wantthem to open in "tiles".. Dorothy Windows XP New Users 5 14th Jan 2005 04:41 AM
key "S" & "SPACE" have function as caps lock & num lock =?Utf-8?B?UGFrYW5law==?= Windows XP Hardware 1 20th Oct 2004 08:19 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:22 PM.