对不起,问题仍然存在 [英] Sorry, the question remains

查看:79
本文介绍了对不起,问题仍然存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码来自MSDN示例。

该示例包含临时行。然而

我不明白使用while循环。因为循环中的脉冲是
,我希望循环

在出列后结束。如果这是正确的,这将是一次性的情况,因此我不明白为什么
使用
a循环。


示例中有两个线程。


public void SecondThread()

{

lock(m_smplQueue)

{

Monitor.Pulse(m_smplQueue);

while(Monitor.Wait(m_smplQueue,1000))

{

int counter =(int)m_smplQueue.Dequeue();

Console.WriteLine(counter.ToString());

Monitor.Pulse( m_smplQueue);

}

}

}

The code below was taken from an MSDN example.
The example contains explantory lines. Nevertheless
I do not understand the use of the while loop. Because
of the Pulse within the loop, I would expect the loop
to end after the dequeue. If that is correct this would
be one off situation, I therefore don''t understand why
a loop was used.

There are two threads in the example.

public void SecondThread()
{
lock(m_smplQueue)
{
Monitor.Pulse(m_smplQueue);
while(Monitor.Wait(m_smplQueue,1000))
{
int counter = (int)m_smplQueue.Dequeue();
Console.WriteLine(counter.ToString());
Monitor.Pulse(m_smplQueue);
}
}
}

推荐答案

我不相信while循环用于遍历

队列中的项目;原MSDN文章中的两条注释行真正解释了while循环正在做什么:
//等待循环,队列忙。

//当第一个线程停止时退出超时。


基本上,while循环导致执行暂停(在当前

线程),直到当前线程可以在m_smplQueue对象上获得独占锁定

,或者1秒钟的时间过去(以发生者为准)

第一)。这是由Monitor.Wait(m_smplQueue,1000)调用执行的。

如果实现了独占锁,则循环内的代码运行。如果

没有实现独占锁,基本上,该方法结束。


以下是完整示例源的链接:
http:/ /msdn.microsoft.com/library/de...pulsetopic.asp


以及Monitor.Wait方法的描述:
http:// msdn.microsoft.com/library/de ... .WaitTopic2.asp


HTH ...

Chris
I don''t believe the while loop is used to iterate through items in the
queue; the two comment lines in the original MSDN article really explain
what the while loop is doing:

//Wait in the loop, while the queue is busy.
//Exit on the time-out when the first thread stops.

Basically, the while loop is causing a halt in execution (on the current
thread), until such time as the current thread can get an exclusive lock
on the m_smplQueue object, or 1 second of time passes (whichever occurs
first). This is carried out by the Monitor.Wait(m_smplQueue,1000) call.
If an exclusive lock is acheived, the code inside the loop runs. If
an exclusive lock is NOT achieved, essentially, the method ends.

Here is a link to the full sample source:
http://msdn.microsoft.com/library/de...pulsetopic.asp

And, the description of the Monitor.Wait method:
http://msdn.microsoft.com/library/de...WaitTopic2.asp

HTH...
Chris


出队后,线程循环返回等待下一回合。


" Zach"写道:
After the dequeue, the thread loops back to wait for its next turn again.

"Zach" wrote:
下面的代码来自MSDN示例。
该示例包含临时行。然而
我不明白使用while循环。因为循环中的脉冲,我希望循环
在出列后结束。如果这是正确的,这将是一个关闭的情况,因此我不明白为什么使用循环。

示例中有两个线程。 />
public void SecondThread()
{
lock(m_smplQueue)
{Monitor.Pulse(m_smplQueue);
while(Monitor.Wait( m_smplQueue,1000))
{
int counter =(int)m_smplQueue.Dequeue();
Console.WriteLine(counter.ToString());
Monitor.Pulse(m_smplQueue );
}
}

The code below was taken from an MSDN example.
The example contains explantory lines. Nevertheless
I do not understand the use of the while loop. Because
of the Pulse within the loop, I would expect the loop
to end after the dequeue. If that is correct this would
be one off situation, I therefore don''t understand why
a loop was used.

There are two threads in the example.

public void SecondThread()
{
lock(m_smplQueue)
{
Monitor.Pulse(m_smplQueue);
while(Monitor.Wait(m_smplQueue,1000))
{
int counter = (int)m_smplQueue.Dequeue();
Console.WriteLine(counter.ToString());
Monitor.Pulse(m_smplQueue);
}
}
}





"涂-西哈奇" <涂***** @ discussions.microsoft.com>在留言中写道

新闻:A7 ********************************** @ microsof t.com ...

"Tu-Thach" <Tu*****@discussions.microsoft.com> wrote in message
news:A7**********************************@microsof t.com...
出队后,线程循环返回等待下一回合。
After the dequeue, the thread loops back to wait for its next turn again.




我认为这便士终于放弃了:

由于Pulse,等待状态保持不变,而另一个线程被激活的同时处于

。在另一个线程脉冲之后,循环继续

。对吗?



I think the penny has finally dropped:
Because of the Pulse, the wait-status is maintained, whilst at
the same time the other thread is activated. The loop continues
after the other thread Pulses. Right?


这篇关于对不起,问题仍然存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆