473,566 Members | 2,785 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Monitor.TryEnte r waittime?

Hello NG,

is there a possibillity to determine how long a thread had to wait on
Monitor.TryEnte r(lockObj,timeo ut) ?

Thanks for hints.
Rainer Queck
Nov 6 '07 #1
5 3816
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.TryEnte r(lockObj,timeo ut) ?
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.TryEnte r(lockObj,timeo ut) ?
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.TryEnte r(lockObj,timeo ut);
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.TryE nter(lockObj,ti meout) )
{
long ticks = StopWatch.Elaps edTicks();
DoTimeConsuming AccessToResourc e();
}

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.co mschrieb im Newsbeitrag
news:11******** **************@ 50g2000hsm.goog legroups.com...
On Nov 6, 1:54 pm, "Rainer Queck" <Rai...@noemail .noemailwrote:
>but would it be impossible to determine who does the
StopWatch.Rese t/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
1872
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, so my understanding of Monitor indicates this would not protect my object. Example: Bitmap bmp = new Bitmap("x.jpg"); Monitor.Enter(bmp); bmp =...
3
9280
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 settings for the monitor I can move around the monitors so if say the second monitor is lower than the first I can position it accordingly. Are...
4
3176
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 interchangeable, or when should I use one over the other? TIA Charles
8
1711
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 .NET 2.0 framework. Note that this runs better, but not perfectly, on .NET 1.1. Note: The numbers are line numbers needed to match the callstack...
3
7676
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 second monitor. I tried to set the X position of the dialog box greather thant the first screen resolution, but the dialog box still appear on the first...
2
1700
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 there is problem. in the thread of the deleting, seems that the deleting function entered twice and both try to delete the queue. The first thread...
12
5247
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 block protecting a resource. Thread 2 and 3 also have a SyncLock block protecting the same resource and after executing some code in their blocks...
4
6141
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 The command was processed as an SQL statement because it was not a valid Command Line Processor command. During SQL processing it returned:
5
14804
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 huge problems with slow connect times and heavy resource locking in high concurrency / high transaction volume environments. I have an OLTP with...
0
7666
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...
0
7888
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. ...
1
7644
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7951
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...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3643
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...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2083
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1201
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.