Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Event handling

Event handling

Scheduled Pinned Locked Moved C#
databasecomhelptutorial
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tichaona J
    wrote on last edited by
    #1

    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. }

    OriginalGriffO L _ 3 Replies Last reply
    0
    • T Tichaona J

      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. }

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      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.

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      1 Reply Last reply
      0
      • T Tichaona J

        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. }

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • T Tichaona J

          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. }

          _ Offline
          _ Offline
          _Erik_
          wrote on last edited by
          #4

          Have a look at this article hosted here in CP. The while(true) within the main application thread is what freezes your application. Bye

          OriginalGriffO 1 Reply Last reply
          0
          • _ _Erik_

            Have a look at this article hosted here in CP. The while(true) within the main application thread is what freezes your application. Bye

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #5

            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 the if and else 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 have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            _ 1 Reply Last reply
            0
            • OriginalGriffO OriginalGriff

              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 the if and else 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.

              _ Offline
              _ Offline
              _Erik_
              wrote on last edited by
              #6

              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.

              OriginalGriffO 1 Reply Last reply
              0
              • _ _Erik_

                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.

                OriginalGriffO Offline
                OriginalGriffO Offline
                OriginalGriff
                wrote on last edited by
                #7

                :laugh:

                Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups