Click here to Skip to main content
15,845,436 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
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???
Posted
Updated 22-Mar-11 11:00am
v3

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...
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 22-Mar-11 17:25pm    
Sorry, not quite agree. This is not too complex and makes sense, only OP is confused about wait. See my Answer is you wish.
--SA
Albin Abel 22-Mar-11 22:52pm    
Conceptually I agree with this. Not sure how to implement in .Net. May be SAKryukov's answer helps. My 5
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

C#
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
 
Share this answer
 
v2
Comments
Albin Abel 22-Mar-11 22:43pm    
Expected your answer here. Yes it is here. My 5+
Sergey Alexandrovich Kryukov 22-Mar-11 22:47pm    
Not so hard, I guess... :-)
Thank you, Albin.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900