帮助ManualResetEvent信号... [英] Help with ManualResetEvent signalling...

查看:81
本文介绍了帮助ManualResetEvent信号...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!
我正在使用XML Web Services创建一个聊天程序. 现在正在聊天客户端上...
我对客户端消息验证有疑问...
我使用 ManualResetEvent 对象为另一个验证线程设置事件,以在发送到Web Service之前过滤用户消息. 我打算这样:
我将TextBox组件的 KeyPressed 事件处理为
编组文本验证到另一个线程...
我使用此线程来验证客户端消息(删除任何有礼貌的词,例如
f ***,sh * t,胡说八道,c *** 是由于使用了ManualResetEvent对象的信号...

伪-代码:

private Thread m_ValidationThread = new Thread(DoTextProcessing);

private ManualResetEvent m_KeyPressedEvent = new ManualResetEvent(false);

private void txtMessageOnKeyPress(object sender, KeyPressEventArgs arg) {
  //blah-blah-blah

 m_KeyPressedEvent.Set( );
}

private void DoTextProcessing( ) {

 while(true) {
 m_KeyPressedEvent.WaitOne( );

lock( m_TextToSend) {
string[ ] words = m_TextToSend.Split( new char[ ] { '' '' });
  foreach(string word in words)
   if ( m_ForbiddenWords.Contains(word))
     NotifyClient("Be a good guy!");
 } // end lock

  m_KeyPressedEvent.Reset( );
 } // end while
}



如果用户键入另一个字符,从而m_KeyPressedEvent调用其
怎么办? 再次< bold> Set()方法! 因此 SIGNALLED m_KeyPressedEvent会再次发出信号
无需重置(通过验证线程)....下一个是什么???
可以吗????????????? ?我认为您正在使事情变得更加困难.


我明白了.要使用手动重置,您需要在从m_KeyPressedEvent.Wait返回后立即在被阻止的线程中调用m_KeyPressedEvent.Reset.只要做

 m_KeyPressedEvent.Wait();
m_KeyPressedEvent.Reset(); 



或者,使用自动重置.


立即删除该wait(true),永远不要再做一次!
这不是旋转等待!永远不要使用旋转等待!
EventWaitHandle.Wait正在阻止.线程被操作系统关闭,并使用零CPU时间进入等待状态.换句话说,在Set唤醒操作系统之前,OS永远不会调度此线程进行执行.

—SA


Hi guys!
I''m creating a chat program using XML Web Services..
Now currently on chat client...
I have a question about client side message validating...
I use ManualResetEvent object to set event for another validating thread to filter a user message before sending to Web Service..
I plan this:
I process KeyPressed event of a TextBox component to
marshal text validation to another thread...
I use this thread to validate client messages (remove any offesnsive words like
f***, sh*t, bullshit, c*** due to signalling of ManualResetEvent object...

Pseudo - Code:

private Thread m_ValidationThread = new Thread(DoTextProcessing);

private ManualResetEvent m_KeyPressedEvent = new ManualResetEvent(false);

private void txtMessageOnKeyPress(object sender, KeyPressEventArgs arg) {
  //blah-blah-blah

 m_KeyPressedEvent.Set( );
}

private void DoTextProcessing( ) {

 while(true) {
 m_KeyPressedEvent.WaitOne( );

lock( m_TextToSend) {
string[ ] words = m_TextToSend.Split( new char[ ] { '' '' });
  foreach(string word in words)
   if ( m_ForbiddenWords.Contains(word))
     NotifyClient("Be a good guy!");
 } // end lock

  m_KeyPressedEvent.Reset( );
 } // end while
}



What if a user types another char and thus m_KeyPressedEvent would call its
<bold>Set( ) method again!!!!!
So a SIGNALLED m_KeyPressedEvent would signal again
without being reset (by validation thread)....what''s the next???
Is it okay???

解决方案

Why not just wait to validate the string until the user presses the enter key, and then make the validation part of the send thread processing? I think you''re making it more difficult than it needs to be...


I see. To use manual reset, you need to call m_KeyPressedEvent.Reset in the blocked thread right after return from m_KeyPressedEvent.Wait. Just do

m_KeyPressedEvent.Wait();
m_KeyPressedEvent.Reset();



Alternatively, use auto-reset.

[EDIT]
Remove that wait(true), immediately, never do it again, ever!
This is not a spin wait! Never use spin wait!
EventWaitHandle.Wait is blocking. The thread is switched off by OS and put to a wait state using zero CPU time. In other words, OS never schedule this thread for executions until it is awaken by Set.

—SA


这篇关于帮助ManualResetEvent信号...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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