About sockets. Receive methods

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Diego F.

    #1

    About sockets. Receive methods

    Hello. I'm using the socket class in an application and I use socket.receive
    to wait for data. The problem is that the interface gets locked while the
    receive method is executing.

    I want to see the interface so the user knows what's happening at any time.

    Is that possible?

    --

    Regards,

    Diego F.



  • Mike Spike

    #2
    Re: About sockets. Receive methods

    You should put the socket code in another thread.

    That way your GUI thread will be free to repaint and update while you are
    looping / waiting to read from the socket.

    I have not used sockets in dot net but that kinda a general rule to keep
    things that loop alot off the GUI thread. Depending on how the sockets are
    blocking or non blocking makes a diffrence but you might be able to put
    application.doe vents or ProcessMessages s or what ever makes the application
    yeild to process windows messages. if you put that within your loop it
    should help, but its a hack rather than a fix.

    Mike.

    "Diego F." <diego_frNO@msn .comwrote in message
    news:ODh5UrdnHH A.4552@TK2MSFTN GP04.phx.gbl...
    Hello. I'm using the socket class in an application and I use
    socket.receive to wait for data. The problem is that the interface gets
    locked while the receive method is executing.
    >
    I want to see the interface so the user knows what's happening at any
    time.
    >
    Is that possible?
    >
    --
    >
    Regards,
    >
    Diego F.
    >
    >
    >

    Comment

    • Tom Shelton

      #3
      Re: About sockets. Receive methods

      On May 24, 2:08 am, "Diego F." <diego_f...@msn .comwrote:
      Hello. I'm using the socket class in an application and I use socket.receive
      to wait for data. The problem is that the interface gets locked while the
      receive method is executing.
      >
      I want to see the interface so the user knows what's happening at any time.
      >
      Is that possible?
      >
      --
      >
      Regards,
      >
      Diego F.
      Diego,

      Recieve is a synchronous socket operation, therefore it will block
      until it recieves data or one of the endpoints is disconnected. You
      have two choices if you don't like that behavior.

      1) Manually spin off a separate thread of execution to perform you
      socket operations.

      2) Use the BeginXXX socket methods for asynchronous behavior.

      IMHO, #2 is the far superior choice. You can find more information
      about general usage of sockets in .net here:

      http://msdn2.microsoft.com/en-us/library/b6xa24z5.aspx

      Here is a specific article on asynchronous client sockets:

      http://msdn2.microsoft.com/en-us/library/bbx2eya8.aspx

      HTH

      --
      Tom Shelton

      Comment

      • Diego F.

        #4
        Re: About sockets. Receive methods

        Thank you for your responses. I tried using a second thread, but then I
        can't access the UI controls (I get an execution exception), so I'll try the
        BeginAccept solution.

        --

        Regards,

        Diego F.


        "Diego F." <diego_frNO@msn .comwrote in message
        news:ODh5UrdnHH A.4552@TK2MSFTN GP04.phx.gbl...
        Hello. I'm using the socket class in an application and I use
        socket.receive to wait for data. The problem is that the interface gets
        locked while the receive method is executing.
        >
        I want to see the interface so the user knows what's happening at any
        time.
        >
        Is that possible?
        >
        --
        >
        Regards,
        >
        Diego F.
        >
        >
        >

        Comment

        • Diego F.

          #5
          Re: About sockets. Receive methods

          I'm thinking about the asynchronous solution, but, it will continue the code
          in the main thread. I'm not sure that it works for me. The code after the
          Receive, is for managing the sent data.

          Do you see my problem? I'm migrating an old VB6 application, and it uses the
          DataArrival event.

          --

          Regards,

          Diego F.


          "Diego F." <diego_frNO@msn .comwrote in message
          news:%23PAx$8gn HHA.4628@TK2MSF TNGP06.phx.gbl. ..
          Thank you for your responses. I tried using a second thread, but then I
          can't access the UI controls (I get an execution exception), so I'll try
          the BeginAccept solution.
          >
          --
          >
          Regards,
          >
          Diego F.
          >
          >
          "Diego F." <diego_frNO@msn .comwrote in message
          news:ODh5UrdnHH A.4552@TK2MSFTN GP04.phx.gbl...
          >Hello. I'm using the socket class in an application and I use
          >socket.recei ve to wait for data. The problem is that the interface gets
          >locked while the receive method is executing.
          >>
          >I want to see the interface so the user knows what's happening at any
          >time.
          >>
          >Is that possible?
          >>
          >--
          >>
          >Regards,
          >>
          >Diego F.
          >>
          >>
          >>
          >
          >

          Comment

          • Tom Shelton

            #6
            Re: About sockets. Receive methods

            On May 24, 8:40 am, "Diego F." <diego_f...@msn .comwrote:
            I'm thinking about the asynchronous solution, but, it will continue the code
            in the main thread. I'm not sure that it works for me. The code after the
            Receive, is for managing the sent data.
            >
            Do you see my problem? I'm migrating an old VB6 application, and it uses the
            DataArrival event.
            >
            --
            >
            Regards,
            >
            Diego F.
            >
            "Diego F." <diego_f...@msn .comwrote in message
            >
            news:%23PAx$8gn HHA.4628@TK2MSF TNGP06.phx.gbl. ..
            >
            >
            >
            Thank you for your responses. I tried using a second thread, but then I
            can't access the UI controls (I get an execution exception), so I'll try
            the BeginAccept solution.
            >
            --
            >
            Regards,
            >
            Diego F.
            >
            "Diego F." <diego_f...@msn .comwrote in message
            news:ODh5UrdnHH A.4552@TK2MSFTN GP04.phx.gbl...
            Hello. I'm using the socket class in an application and I use
            socket.receive to wait for data. The problem is that the interface gets
            locked while the receive method is executing.
            >
            I want to see the interface so the user knows what's happening at any
            time.
            >
            Is that possible?
            >
            --
            >
            Regards,
            >
            Diego F.- Hide quoted text -
            >
            - Show quoted text -
            The Async methods are based on callbacks, so it is going to be similar
            to the way it works with the dataarival event, but with a little more
            housekeeping.

            By the way, either way - manual thread creation or the BeginXXX
            methods, you will be unable to directly access the UI. Both methods
            cause a thread creation, it's just using the async methods take care
            of it and are very effiecient. So, you may want to look at this
            series of articles on windows forms and multithreading:

            http://msdn2.microsoft.com/en-us/library/ms951089.aspx

            That's a link to the first part - but it is a 3 part article, so read
            it all :)

            The other option is to use the sync method, and use a backgroundworke r
            component... Or find a 3rd party component.

            --
            Tom Shelton


            Comment

            • Michael M.

              #7
              Re: About sockets. Receive methods

              In terms of accessing the GUI from network threads i some times do this :

              Hook the windows with the controls then when I want to update the GUI is use
              POSTMESSAGE

              This way the message is sent to the Window Loop which is hosting the GUI
              controls, the Loop is running inside the GUI thread anyway so no access
              violation occurs (accessing the controls via proxy).

              If it is a complex control you can send your own message constant to the
              main window loop like POSTMESSAGE(for m1.hwnd, WM_LogConnectio n_Request, 0,
              PointerToString )

              then process this message

              if Msg.Message = WM_LogConnectio n_Request then
              ListBox1.Item.A dd(Translated_S tring_from_poin ter)

              end if



              "Diego F." <diego_frNO@msn .comwrote in message
              news:%23PAx$8gn HHA.4628@TK2MSF TNGP06.phx.gbl. ..
              Thank you for your responses. I tried using a second thread, but then I
              can't access the UI controls (I get an execution exception), so I'll try
              the BeginAccept solution.
              >
              --
              >
              Regards,
              >
              Diego F.
              >
              >
              "Diego F." <diego_frNO@msn .comwrote in message
              news:ODh5UrdnHH A.4552@TK2MSFTN GP04.phx.gbl...
              >Hello. I'm using the socket class in an application and I use
              >socket.recei ve to wait for data. The problem is that the interface gets
              >locked while the receive method is executing.
              >>
              >I want to see the interface so the user knows what's happening at any
              >time.
              >>
              >Is that possible?
              >>
              >--
              >>
              >Regards,
              >>
              >Diego F.
              >>
              >>
              >>
              >
              >

              Comment

              Working...