473,947 Members | 1,768 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Please explain

Could someone please explain what the
consequence of this //****** line is?

public void SecondThread()
{
lock(m_smplQueu e)
{
Monitor.Pulse(m _smplQueue);
while(Monitor.W ait(m_smplQueue ,1000))//******
{
int counter = (int)m_smplQueu e.Dequeue();
Console.WriteLi ne(counter.ToSt ring());
Monitor.Pulse(m _smplQueue);
}
}
}
Nov 16 '05 #1
6 1533
Hi,

It instructs to wait for EITHER m_smplQueue to become signaled OR the one
second timeout to occur. If the Wait calls returns due to timeout, it
returns false, thus preventing the execution flow from entering the while
loop.

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

"Zach" <no*@this.addre ss> wrote in message
news:ce******** *************** ****@freeler.nl ...
Could someone please explain what the
consequence of this //****** line is?

public void SecondThread()
{
lock(m_smplQueu e)
{
Monitor.Pulse(m _smplQueue);
while(Monitor.W ait(m_smplQueue ,1000))//******
{
int counter = (int)m_smplQueu e.Dequeue();
Console.WriteLi ne(counter.ToSt ring());
Monitor.Pulse(m _smplQueue);
}
}
}


Nov 16 '05 #2
The Monitor class is waiting on the queue object with a timeout of 1000
milliseconds.

This thread and some other(s) are synchronised on the queue object.

From MSDN...

When a thread calls Wait, it releases the lock on the object and enters the
object's waiting queue. The next thread in the object's ready queue (if
there is one) acquires the lock and has exclusive use of the object. The
thread that invoked Wait remains in the waiting queue until either a thread
that holds the lock invokes PulseAll, or it is the next in the queue and a
thread that holds the lock invokes Pulse. However, if millisecondsTim eout
elapses before another thread invokes this object's Pulse or PulseAll
method, the original thread is moved to the ready queue in order to regain
the lock. If the condition in the object's state has not been met, the
thread might call Wait again to reenter the waiting queue until it has been
met.
--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"Zach" <no*@this.addre ss> wrote in message
news:ce******** *************** ****@freeler.nl ...
Could someone please explain what the
consequence of this //****** line is?

public void SecondThread()
{
lock(m_smplQueu e)
{
Monitor.Pulse(m _smplQueue);
while(Monitor.W ait(m_smplQueue ,1000))//******
{
int counter = (int)m_smplQueu e.Dequeue();
Console.WriteLi ne(counter.ToSt ring());
Monitor.Pulse(m _smplQueue);
}
}
}

Nov 16 '05 #3
"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.c om> wrote
in message news:OU******** ******@TK2MSFTN GP14.phx.gbl...
Hi,

It instructs to wait for EITHER m_smplQueue to become signaled OR the one
second timeout to occur. If the Wait calls returns due to timeout, it
returns false, thus preventing the execution flow from entering the while
loop.


As I understand the code, the contents of the loop
will be activated when waiting is true. But that will
happen only once because of the Pulse. Since it is
a one-off action, I fail to see the use of the "while".

And, please, what is the difference (apart from the
time-out) between:

(A)

Monitor.Pulse(m _smplQueue);
while(Monitor.W ait(m_smplQueue ,1000))//******
{
int counter = (int)m_smplQueu e.Dequeue();
Console.WriteLi ne(counter.ToSt ring());
Monitor.Pulse(m _smplQueue);
}

and:

(B)

(there are MAX elements in the queue)

Monitor.Pulse(m _smplQueue);
int ctr = 0;
while(ctr++ < MAX)
{
Monitor.Wait(m_ smplQueue);
int counter = (int)theQueue.D equeue();
Console.WriteLi ne(counter.ToSt ring());
Monitor.Pulse(m _smplQueue);
}

Nov 16 '05 #4

"Bob Powell [MVP]" <bob@_spamkille r_bobpowell.net > wrote in message
news:#0******** ******@TK2MSFTN GP15.phx.gbl...
The Monitor class is waiting on the queue object with a timeout of 1000
milliseconds.

This thread and some other(s) are synchronised on the queue object.

From MSDN...

When a thread calls Wait, it releases the lock on the object and enters the object's waiting queue. The next thread in the object's ready queue (if
there is one) acquires the lock and has exclusive use of the object. The
thread that invoked Wait remains in the waiting queue until either a thread that holds the lock invokes PulseAll, or it is the next in the queue and a
thread that holds the lock invokes Pulse. However, if millisecondsTim eout
elapses before another thread invokes this object's Pulse or PulseAll
method, the original thread is moved to the ready queue in order to regain
the lock. If the condition in the object's state has not been met, the
thread might call Wait again to reenter the waiting queue until it has been met.


Hi Bob,

So Wait = GOTO WaitQ
So Pulse = LEAVE WaitQ + GOTO ReadyQ

while (waiting, but stop waiting after a second)
{
if(it is true that you are waiting) do something
Pulse = stop doing the something
}

Why a loop? The action takes place only once?

Zach.
Nov 16 '05 #5
This code you've posted comes directly from the Monitor sample where it is
well commented as to exactly what's going on.

See MSDN and the Monitor.Wait method. The code in that page documents
exactly what's happening.

Did you get your code from somewhere other than MSDN? If you did, whoever
wrote it just copied the sample and took out the useful bits, ie, the
comments.
--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"Zach" <00@00.00> wrote in message
news:cc******** *************** ****@freeler.nl ...

"Bob Powell [MVP]" <bob@_spamkille r_bobpowell.net > wrote in message
news:#0******** ******@TK2MSFTN GP15.phx.gbl...
The Monitor class is waiting on the queue object with a timeout of 1000
milliseconds.

This thread and some other(s) are synchronised on the queue object.

From MSDN...

When a thread calls Wait, it releases the lock on the object and enters

the
object's waiting queue. The next thread in the object's ready queue (if
there is one) acquires the lock and has exclusive use of the object. The
thread that invoked Wait remains in the waiting queue until either a

thread
that holds the lock invokes PulseAll, or it is the next in the queue and a thread that holds the lock invokes Pulse. However, if millisecondsTim eout elapses before another thread invokes this object's Pulse or PulseAll
method, the original thread is moved to the ready queue in order to regain the lock. If the condition in the object's state has not been met, the
thread might call Wait again to reenter the waiting queue until it has

been
met.


Hi Bob,

So Wait = GOTO WaitQ
So Pulse = LEAVE WaitQ + GOTO ReadyQ

while (waiting, but stop waiting after a second)
{
if(it is true that you are waiting) do something
Pulse = stop doing the something
}

Why a loop? The action takes place only once?

Zach.

Nov 16 '05 #6
"Bob Powell [MVP]" <bob@_spamkille r_bobpowell.net > wrote in message
news:#I******** ******@tk2msftn gp13.phx.gbl...
This code you've posted comes directly from the Monitor sample where it is
well commented as to exactly what's going on.


I understand that {the action}depends on waiting, which terminates
immediately due to the subsequent Pulse. So the action is one-off,
so why a loop. That is my question, in spite of the excellent comment
in MSDN which I have seen, and do not understand.
Nov 16 '05 #7

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

Similar topics

9
3556
by: DD | last post by:
Hello, Could anyone please help me?? Is there somebody who could explain me how to make a connection to a access database with a python cgi script. I would like to use common sql commands in my python scripts as I can with MySQLdb. But I cannot even connect to the access database (see below). Could anyone explain it to me as simple as possible please. I'm using Windows XP, ActivePython 2.3.2 build 230 and Microsoft access(XP?)
3
3207
by: VijayShankar | last post by:
Can u be more specific on your question Anyway its not like Session variables are available for sometime and not available for sometime. When your session starts it is very much available unless your session ends One more thing Session variables can very much be used in Application events
1
4527
by: Yash | last post by:
Hi, Can someone please explain to me what the StreamReader.DiscardBufferedData method does? The documentation says "Use DiscardBufferedData to seek to a known location in the underlying stream and then begin reading from this new point, or to read the contents of a StreamReader more than once." I am not able to understand what exactly this means.
5
3623
by: KathyB | last post by:
If someone could just explain this to me...I just don't get it! I have an aspx page where I retrieve several session variables and use xmlDocument to transform xml file with xsl file into an instruction document (not data based) - same as using an xml web control. The resulting html is on the client? but what about the server side of things? Trying to figure out how to change and save the xmlDocument. It I put a button OUTSIDE of the...
12
3099
by: Sanjeev | last post by:
Output of followin program at Turbo C++ 3.0 is 7 ( Not 2 or 3). Please explain why ? //////////////////////////////////////////////// #include<stdio.h> #include<string.h> void main() {
2
2223
by: garyusenet | last post by:
I could do with something similiar, can you tell me if you think this would work for me, and if there's any advantage in working with controls this way than how I currently am. At the moment i'm using the treenodes and each treenode needs a unique entry into my rich text box. After sitting at home with MSDN i've managed to get this functionality by storing a RTF string in the tag property of the treenode. On every 'before update' of the...
9
2119
by: colin.mcnulty | last post by:
Hi, I'm a SQL Server DBA, but I guess that won't buy me any friends round here huh? ;-) I've been asked to look at the SQL that's being executed on a DB2 database from a web app, specifically when the web site does XYZ, what SQL does it run on the DB2 database? Unfortunately everyone who knew about how it works has left and I've never even seen a DB2 database before today! So, I appear to be looking at an IBM DB2 Universal Database...
61
3662
by: warint | last post by:
My lecturer gave us an assignment. He has a very "mature" way of teaching in that he doesn't care whether people show up, whether they do the assignments, or whether they copy other people's work. Furthermore, he doesn't even mark the assignments, but rather gives tips and so forth when going over students' work. To test students' capabilities for the purpose of state exams and qualifications though, he actually sits down with us at a...
3
1475
by: sathishc58 | last post by:
Hi All, Here is the code which generates Segmentation Fault. Can anyone explain why the third printf fails and the first printf works? main() { char ch={"Hello"}; char *p; p=ch; printf("Character is %c\n", *p);
2
2201
by: sathishc58 | last post by:
Hi All Please explain why strlen returns() "16" as output here and explain the o/p for sizeof() as well main() { char a={'a','b','c'}; printf("strlen=%d\n", strlen(a)); printf("sizeof=%d\n", sizeof(a)); printf("%d %d", strlen(a),sizeof a);
0
10163
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9982
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,...
1
11347
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
10692
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
8255
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
7430
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6116
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...
1
4946
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
2
4538
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.