|
-
October 21st, 2010, 12:55 AM
#1
ManualResetEvent
Hello , i am trying to make an asyncourns communicaition chat , mostly it works fine untill i come across a deadlock wich i cant explain its cuase , here are some code snippets to argue my case ,
i am new to programming and might of not understood how to use the manuaulresetevent correctly
ManualResetEvent receiveDone ;
/*(1) this is part of the code after the "client" as already connected ,it then receives its own thread to run in the thread initaits a delegate method "HandleClient" in wich it will listen for incoming messages (1)*/
new Thread(new ParameterizedThreadStart(HandleClient)).Start(Clients.ElementAt(Clients.Count - 1));
.
.
(2) /* THE BIG QUESTION is receiveDone a new instance in every thread or is it the same instance wich gets overriden when a new thread is dispatched ? */
public void HandleClient(object obj)
{
receiveDone = new ManualResetEvent(false); // manual Recent event is declered at the beging of the code , this might be the problem that receiveDone would point to a new instance every time its declerd or is it as if it as its own instance wich stays its self in every thread i dispatch...?
i need it decleard on top so i could use it in the asyncallback method ReceiveCallBack
do {
receiveDone.Reset();
currentClient.netstream.BeginRead(currentClient.ReceiveBuffer, 0, currentClient.ReceiveBuffer.Length, new AsyncCallback(ReceiveCallBack), currentClient.netstream);
receiveDone.WaitOne();
/* SOME MORE CODE */
}while(netstream.canRead()) ;
private void ReceiveCallBack(IAsyncResult ar)
{
NetworkStream ns = (NetworkStream)ar.AsyncState;
ns.EndRead(ar);
receiveDone.Set();
}
-
October 21st, 2010, 02:39 PM
#2
Re: ManualResetEvent
(2) /* THE BIG QUESTION is receiveDone a new instance in every thread or is it the same instance wich gets overriden when a new thread is dispatched ? */
If HandleClient is called more than once, then receiveDone is going to get overwritten with a new manual reset event each time HandleClient is called. If you only want a single receiveDone event, initialize it inline or in the constructor.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|