473,406 Members | 2,378 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Monitor.TryEnter waittime?

Hello NG,

is there a possibillity to determine how long a thread had to wait on
Monitor.TryEnter(lockObj,timeout) ?

Thanks for hints.
Rainer Queck
Nov 6 '07 #1
5 3803
On Nov 6, 12:40 pm, "Rainer Queck" <Rai...@noemail.noemailwrote:
is there a possibillity to determine how long a thread had to wait on
Monitor.TryEnter(lockObj,timeout) ?
Only by timing it - e.g. using the Stopwatch class.

Jon

Nov 6 '07 #2
Rainer Queck wrote:
Hello NG,

is there a possibillity to determine how long a thread had to wait on
Monitor.TryEnter(lockObj,timeout) ?
You could take a snapshot of DateTime.Now both before and after it
acquires the lock, subtract them and interrogate the resulting TimeSpan:

DateTime startAcquire = DateTime.Now;
Monitor.TryEnter(lockObj,timeout);
DateTime endAcquire = DateTime.Now;

TimeSpan waitTime = endAcquire - startAcquire;
Chris.
Nov 6 '07 #3
Hi Jon, Hi Chris,

but would it be impossible to determine who does the StopWatch.Reset/Start
(or DateTime.Now) and who reads it?

Excample

StopWatch.Reset();
StopWatch.Start();
if(Monitor.TryEnter(lockObj,timeout) )
{
long ticks = StopWatch.ElapsedTicks();
DoTimeConsumingAccessToResource();
}

lets assume
thread1 comes. No Problem
while thread 1 is still working inside thread two come and has to wait.
Still no problem
BUT now it thread3 comes allong thread 1 is STILL working, it will do a
reset of the StopWatch before it starts waiting, won't it?
Now if thread2 can enter, ticks will have a wrong value true?

Regards
Rainer
Nov 6 '07 #4
On Nov 6, 1:54 pm, "Rainer Queck" <Rai...@noemail.noemailwrote:
but would it be impossible to determine who does the StopWatch.Reset/Start
(or DateTime.Now) and who reads it?
You'd create a separate Stopwatch for that particular call - only that
thread would even know about it.

Jon

Nov 6 '07 #5
Thanks for that hint.
I wasn't sure that local vars are "on the stack".

Regards
Rainer
"Jon Skeet [C# MVP]" <sk***@pobox.comschrieb im Newsbeitrag
news:11**********************@50g2000hsm.googlegro ups.com...
On Nov 6, 1:54 pm, "Rainer Queck" <Rai...@noemail.noemailwrote:
>but would it be impossible to determine who does the
StopWatch.Reset/Start
(or DateTime.Now) and who reads it?

You'd create a separate Stopwatch for that particular call - only that
thread would even know about it.

Jon

Nov 6 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Brett Robichaud | last post by:
I need to make access to a reference object threadsafe. My natural instinct was to simply use Monitor.Enter() and Exit(). The problem is that the object behind the reference changes frequently,...
3
by: slowram | last post by:
I know how to get the monitor dimensions, virtual monitor dimensions, but how do I get the location of the second monitor in relationship to the original? On the desktop when I go into my property...
4
by: Charles Law | last post by:
I've been using monitors a bit lately (some of you may have heard ;-) ) and then up pop Manual and AutoResetEvents , and they look for all the world like the same thing. Are they...
8
by: Michael Kennedy | last post by:
Hi, I have been looking into the claim that the keyword lock is not safe when exceptions are possible. That lead me to try the following code, which I think has uncovered a serious error in the...
3
by: fournij | last post by:
Hi I'm writing a MFC C++ application using Visual .NET. I'm using a Dual Head video card with 2 monitors. I want to start my application in the first monitor but open a dialog box in the...
2
by: www.brook | last post by:
I have a VC++.net code. Two threads are created, one thread keeps appending elements to the queue, another keeps deleting an element from the queue. I tried to use monitor, but it seems that the...
12
by: Perecli Manole | last post by:
I am having some strange thread synchronization problems that require me to better understand the intricacies of Monitor.Wait/Pulse. I have 3 threads. Thread 1 does a Monitor.Wait in a SyncLock...
4
by: Raj | last post by:
Can we create an event monitor for statements in a partitioned environment? CREATE EVENT MONITOR stmt_event FOR STATEMENTS WRITE TO FILE '/home/rajm/event' ON PARTITION 0 GLOBAL DB21034E ...
5
by: jbenner | last post by:
I have opened a PMR for this with IBM, and am not asking for advice from the DB2 DBA community. I am posting this as an FYI that DB2 Health Monitor, even at the latest version of DB2, still can cause...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...

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.