Event handling
-
Last time I used an event handler was to detect when a given track that is being played by the media element had ended, which was not difficult simply because the media element has an event called media ended. (http://msdn.microsoft.com/en-us/library/system.windows.controls.mediaelement.mediaended.aspx[^]) However what if the class your working with don't have events, I am working with the socket class and would like to set an event handler to listen for any incoming connection. So for as you can see from the code below I am using a button click event to trigger the application to begin listneing, which isn't that I want. I would like the application to start listening for in coming connections from the get go. How to I do this: private void BtnListen_Click(object sender, RoutedEventArgs e) { Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); listener.Bind(new IPEndPoint(IPAddress.Any, 2112)); listener.Listen(10); while (true) { Socket socket = listener.Accept(); string receivedValue = string.Empty; byte[] receivedBytes = new byte[1024]; int numBytes = socket.Receive(receivedBytes); receivedValue = Encoding.ASCII.GetString(receivedBytes, 0, numBytes); if (receivedValue.Length < -1) { break; } else { MessageBox.Show(receivedValue); break; } listener.Shutdown(SocketShutdown.Both); listener.Close(); } listener.Close(); Another problem I am having is that once the listen button is clicked, the application freezes until it receives something. }
-
Last time I used an event handler was to detect when a given track that is being played by the media element had ended, which was not difficult simply because the media element has an event called media ended. (http://msdn.microsoft.com/en-us/library/system.windows.controls.mediaelement.mediaended.aspx[^]) However what if the class your working with don't have events, I am working with the socket class and would like to set an event handler to listen for any incoming connection. So for as you can see from the code below I am using a button click event to trigger the application to begin listneing, which isn't that I want. I would like the application to start listening for in coming connections from the get go. How to I do this: private void BtnListen_Click(object sender, RoutedEventArgs e) { Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); listener.Bind(new IPEndPoint(IPAddress.Any, 2112)); listener.Listen(10); while (true) { Socket socket = listener.Accept(); string receivedValue = string.Empty; byte[] receivedBytes = new byte[1024]; int numBytes = socket.Receive(receivedBytes); receivedValue = Encoding.ASCII.GetString(receivedBytes, 0, numBytes); if (receivedValue.Length < -1) { break; } else { MessageBox.Show(receivedValue); break; } listener.Shutdown(SocketShutdown.Both); listener.Close(); } listener.Close(); Another problem I am having is that once the listen button is clicked, the application freezes until it receives something. }
Look at Socket.BeginReceive (MSDN)[^] It should solve both problems if you read the documentation.
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
-
Last time I used an event handler was to detect when a given track that is being played by the media element had ended, which was not difficult simply because the media element has an event called media ended. (http://msdn.microsoft.com/en-us/library/system.windows.controls.mediaelement.mediaended.aspx[^]) However what if the class your working with don't have events, I am working with the socket class and would like to set an event handler to listen for any incoming connection. So for as you can see from the code below I am using a button click event to trigger the application to begin listneing, which isn't that I want. I would like the application to start listening for in coming connections from the get go. How to I do this: private void BtnListen_Click(object sender, RoutedEventArgs e) { Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); listener.Bind(new IPEndPoint(IPAddress.Any, 2112)); listener.Listen(10); while (true) { Socket socket = listener.Accept(); string receivedValue = string.Empty; byte[] receivedBytes = new byte[1024]; int numBytes = socket.Receive(receivedBytes); receivedValue = Encoding.ASCII.GetString(receivedBytes, 0, numBytes); if (receivedValue.Length < -1) { break; } else { MessageBox.Show(receivedValue); break; } listener.Shutdown(SocketShutdown.Both); listener.Close(); } listener.Close(); Another problem I am having is that once the listen button is clicked, the application freezes until it receives something. }
Hi, 1. I didn't look at your code as it isn't formatted properly. Please use PRE tags for code snippets. 2. Communication stuff should not be handled synchronously on the main thread; either use asynchronous "non-blocking" calls, or, better yet, use a separate thread. That will stop your GUI freezing problem. 3. When you want your button-click code to execute right away, then don't put it in a button_click handler! Why not put it in your Form's constructor, its Load event handler, or, best, its Shown handler. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
Last time I used an event handler was to detect when a given track that is being played by the media element had ended, which was not difficult simply because the media element has an event called media ended. (http://msdn.microsoft.com/en-us/library/system.windows.controls.mediaelement.mediaended.aspx[^]) However what if the class your working with don't have events, I am working with the socket class and would like to set an event handler to listen for any incoming connection. So for as you can see from the code below I am using a button click event to trigger the application to begin listneing, which isn't that I want. I would like the application to start listening for in coming connections from the get go. How to I do this: private void BtnListen_Click(object sender, RoutedEventArgs e) { Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); listener.Bind(new IPEndPoint(IPAddress.Any, 2112)); listener.Listen(10); while (true) { Socket socket = listener.Accept(); string receivedValue = string.Empty; byte[] receivedBytes = new byte[1024]; int numBytes = socket.Receive(receivedBytes); receivedValue = Encoding.ASCII.GetString(receivedBytes, 0, numBytes); if (receivedValue.Length < -1) { break; } else { MessageBox.Show(receivedValue); break; } listener.Shutdown(SocketShutdown.Both); listener.Close(); } listener.Close(); Another problem I am having is that once the listen button is clicked, the application freezes until it receives something. }
Have a look at this article hosted here in CP. The while(true) within the main application thread is what freezes your application. Bye
-
Have a look at this article hosted here in CP. The while(true) within the main application thread is what freezes your application. Bye
I think you will find it's not:
int numBytes = socket.Receive(receivedBytes);
Receive is a blocking call, so it will not return until the receive is complete. He has a
break
on each of theif
andelse
conditions which would terminate his loop, if it ever got that far...Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
-
I think you will find it's not:
int numBytes = socket.Receive(receivedBytes);
Receive is a blocking call, so it will not return until the receive is complete. He has a
break
on each of theif
andelse
conditions which would terminate his loop, if it ever got that far...Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
-
You are absolutely right. I was just trying to avoid the next question once he finds out how to receive asynchronously: Why is my application still freezing? Ok, just kidding. The truth is that I did not pay attention to the code.
:laugh:
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.