Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / October 2006

Tip: Looking for answers? Try searching our database.

monitor.tryenter

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
gadya - 19 Oct 2006 16:59 GMT
I am using monitor.tryenter as follows

Do Until Monitor.TryEnter(lockLastBets, 10)
                                   moni += 1
                                   sendMsg("PMB std" &
Thread.CurrentThread.Name & " failed " & moni & " times", timesOther)
Loop

It appears that (occasionally?) the tryEnter doesn't relinquish control to
the other thread which is holding the lock.

I thought the whole point of TryEnter was to give a chance for the other
lock holder to finish his job and release the lock. After n failures TryEnter
returns to caller returning false. The caller can then decide what to do. I
send an error msg and go round the loop again.

Have I got it wrong?
Signature

--
Victor

Ben Voigt - 19 Oct 2006 22:17 GMT
>I am using monitor.tryenter as follows
>
[quoted text clipped - 15 lines]
>
> Have I got it wrong?
Just call sleep when you didn't get the lock, in order to let other threads
run.
Göran Andersson - 21 Oct 2006 05:37 GMT
The point of TryEnter is to give your program the opportunity to do
something else while waiting for the lock to go away.

The TryEnter does not try repeatedly to get a lock. If the object is
locked it returns immediately so that your program can make use of the time.

Unless you have a multi processor machine, there is only one thread
running at any time. That means that if the object is locked when your
thread is running, it won't be unlocked until your time slice is over,
and the thread holding the lock gets a chance to finish what it was
doing when locking the object. This in turn means that if you have a
loop what waits for the lock to go away and don't do anything worth wile
in the loop, you will waste your entire time slice waiting for something
that can not happen.

If you are not going to do something while waiting for the lock, use the
Enter method, or at least call Thread.Sleep to give up the rest of the
time slice to other processes.

> I am using monitor.tryenter as follows
>
[quoted text clipped - 13 lines]
>
> Have I got it wrong?

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.