473,698 Members | 2,440 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 3821
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
1879
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 = new Bitmap("y.jpg"); Monitor.Exit(bmp);
3
9301
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 these values wrote to the registry? Is there some way to get them? For instance I'd like to know if...
4
3190
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
1717
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 of the exception below.
3
7685
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 monitor. Can you help me ?
2
1720
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 find it is not empty, and try to delete an element, but before this thread actually delete it,...
12
5266
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 they both do a Monitor.Pulse to hand of the locked resource back to thread 1. While thread 1 has...
4
6159
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
14813
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 30-90 transactions per second activity, and start of Health Monitor every 2 hours was crashing our...
0
8608
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
9029
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8897
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8867
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...
1
6522
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4370
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...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2332
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2006
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.