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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Wait Until Event Occurs

Wait Until Event Occurs

Scheduled Pinned Locked Moved C / C++ / MFC
c++comhostingtutorialquestion
5 Posts 2 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.
  • S Offline
    S Offline
    staticv
    wrote on last edited by
    #1

    Hey, I'm doing sockets programming. Pretty much new to it. The application is a Windows Service, in its ServiceMain() function I call CAsyncSocket's Listen() method, to listen for client connections. But after it starts listening for connections, it'll return and the ServiceMain() function will return and the service is stopped. What I want to do with this is that, wait until a specific event occurs say WM_QUIT, till than listen for connections. How to do it?

    // static member function (callback)
    void CNTService::ServiceMain(DWORD dwArgc, LPTSTR* lpszArgv)
    {
    // m_Server containts two sockets
    // one for listening and one for accepting connections

    m\_Server.StartListening();  // calls Listen method on the listener socket
    
    // it will return and service will quit
    // which I don't want
    

    }

    I'm okay with mixing Win32 and MFC, so if it can be done in Win32 please do tell me too :)

    Top Web Hosting Providers[^] Do, or do not. There is no 'try'.

    S 1 Reply Last reply
    0
    • S staticv

      Hey, I'm doing sockets programming. Pretty much new to it. The application is a Windows Service, in its ServiceMain() function I call CAsyncSocket's Listen() method, to listen for client connections. But after it starts listening for connections, it'll return and the ServiceMain() function will return and the service is stopped. What I want to do with this is that, wait until a specific event occurs say WM_QUIT, till than listen for connections. How to do it?

      // static member function (callback)
      void CNTService::ServiceMain(DWORD dwArgc, LPTSTR* lpszArgv)
      {
      // m_Server containts two sockets
      // one for listening and one for accepting connections

      m\_Server.StartListening();  // calls Listen method on the listener socket
      
      // it will return and service will quit
      // which I don't want
      

      }

      I'm okay with mixing Win32 and MFC, so if it can be done in Win32 please do tell me too :)

      Top Web Hosting Providers[^] Do, or do not. There is no 'try'.

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      Ahmed Manzoor wrote:

      What I want to do with this is that, wait until a specific event occurs say WM_QUIT

      Does the service have a window (and associated window queue) on which to listen for window messages? Doubt it... Instead, use a Win32 IPC event object[^] with a name specific to your service (hint: use an ASCII representation of a UUID to get a unique name) and wait on that with WaitForSingleObject[^].

      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

      S 1 Reply Last reply
      0
      • S Stuart Dootson

        Ahmed Manzoor wrote:

        What I want to do with this is that, wait until a specific event occurs say WM_QUIT

        Does the service have a window (and associated window queue) on which to listen for window messages? Doubt it... Instead, use a Win32 IPC event object[^] with a name specific to your service (hint: use an ASCII representation of a UUID to get a unique name) and wait on that with WaitForSingleObject[^].

        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

        S Offline
        S Offline
        staticv
        wrote on last edited by
        #3

        But, won't that block the thread and everything will stop!

        Top Web Hosting Providers[^] Do, or do not. There is no 'try'.

        S 1 Reply Last reply
        0
        • S staticv

          But, won't that block the thread and everything will stop!

          Top Web Hosting Providers[^] Do, or do not. There is no 'try'.

          S Offline
          S Offline
          Stuart Dootson
          wrote on last edited by
          #4

          Quite true - I was thinking of the other sort of asynchronous IO that uses APCs (but that wouldn't work with WaitForSingleObject - it would need WaitForSingleObjectEx). OK. So, you need a window message pump to handle blocking for you...which is actually something that CSocket provides - whioch leads to the question - why not use CSocket instead of CAsyncSocket? I can't see any advantage to CAsyncSocket if you're only using one socket.... Anyway - to see how to block and process socket notifications, have a look at the CSocket message loop in CSocket::ProcessAuxQueue, which is in sockcore.cpp in your MFC source directory (that's C:\Program Files\Microsoft Visual Studio 9.0\VC\atlmfc\src\mfc for me).

          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

          S 1 Reply Last reply
          0
          • S Stuart Dootson

            Quite true - I was thinking of the other sort of asynchronous IO that uses APCs (but that wouldn't work with WaitForSingleObject - it would need WaitForSingleObjectEx). OK. So, you need a window message pump to handle blocking for you...which is actually something that CSocket provides - whioch leads to the question - why not use CSocket instead of CAsyncSocket? I can't see any advantage to CAsyncSocket if you're only using one socket.... Anyway - to see how to block and process socket notifications, have a look at the CSocket message loop in CSocket::ProcessAuxQueue, which is in sockcore.cpp in your MFC source directory (that's C:\Program Files\Microsoft Visual Studio 9.0\VC\atlmfc\src\mfc for me).

            Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

            S Offline
            S Offline
            staticv
            wrote on last edited by
            #5

            Okay, thanks I'll take a look at the source. Its not that I'm using only one socket :). I'll add support for more connections, but later. I only need blocking for listening and everything else asynchronous. I guess, I'll have to kick-off some threads. Any good strategy for using blocking listening and non-blocking I/O, MULTITHREADEDly :-D

            Top Web Hosting Providers[^] Do, or do not. There is no 'try'.

            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