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 / C++ / MFC
  4. Utility Start/Stop Management Service

Utility Start/Stop Management Service

Scheduled Pinned Locked Moved C / C++ / MFC
toolshelpquestioncareer
8 Posts 3 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.
  • P Offline
    P Offline
    parths
    wrote on last edited by
    #1

    Hi Guys, I am writing a windows service to start / stop certain utility applications. The utilities are in Vb (Please don't ask me why a background process was programmed in vb, I don't know that myself). I used CreateProcess to start them, that was fine. Now the problem arises when I want to stop them. I can't use TerminateProcess because the applications may be in the middle of something and I want to wait till they finish the job at hand. So I decided to use messages. I tried PostThreadMessage. The problem I have was that on the utilities' side, if I used GetMessage then it waits for the message which makes the application hang and if I used PeekMessage then in some cases it was missing the Message altogether. Finally I thought of using a dummy subclassed form and sending the message to to form. It's working now, but it seems like a mess to me. I'm pretty sure there's a better way. Any suggestions? thanks. P "It was when I found out I could make mistakes that I knew I was on to something." -Ornette Coleman

    T R 2 Replies Last reply
    0
    • P parths

      Hi Guys, I am writing a windows service to start / stop certain utility applications. The utilities are in Vb (Please don't ask me why a background process was programmed in vb, I don't know that myself). I used CreateProcess to start them, that was fine. Now the problem arises when I want to stop them. I can't use TerminateProcess because the applications may be in the middle of something and I want to wait till they finish the job at hand. So I decided to use messages. I tried PostThreadMessage. The problem I have was that on the utilities' side, if I used GetMessage then it waits for the message which makes the application hang and if I used PeekMessage then in some cases it was missing the Message altogether. Finally I thought of using a dummy subclassed form and sending the message to to form. It's working now, but it seems like a mess to me. I'm pretty sure there's a better way. Any suggestions? thanks. P "It was when I found out I could make mistakes that I knew I was on to something." -Ornette Coleman

      T Offline
      T Offline
      Toni78
      wrote on last edited by
      #2

      Take a look two or three messages down "how do i Detect if an external program has finished excuting(shellexec)?" and read Rage's and cmk's messages. I think, they apply to you as well. // Afterall, I realized that even my comment lines have bugs When one cannot invent, one must at least improve (in bed).-My latest fortune cookie

      P 1 Reply Last reply
      0
      • T Toni78

        Take a look two or three messages down "how do i Detect if an external program has finished excuting(shellexec)?" and read Rage's and cmk's messages. I think, they apply to you as well. // Afterall, I realized that even my comment lines have bugs When one cannot invent, one must at least improve (in bed).-My latest fortune cookie

        P Offline
        P Offline
        parths
        wrote on last edited by
        #3

        I am waiting for them to finish. Actually, I don't just want to wait for the programs to finish. I actually want to tell them to wrap up whatever they are doing and end. "It was when I found out I could make mistakes that I knew I was on to something." -Ornette Coleman

        1 Reply Last reply
        0
        • P parths

          Hi Guys, I am writing a windows service to start / stop certain utility applications. The utilities are in Vb (Please don't ask me why a background process was programmed in vb, I don't know that myself). I used CreateProcess to start them, that was fine. Now the problem arises when I want to stop them. I can't use TerminateProcess because the applications may be in the middle of something and I want to wait till they finish the job at hand. So I decided to use messages. I tried PostThreadMessage. The problem I have was that on the utilities' side, if I used GetMessage then it waits for the message which makes the application hang and if I used PeekMessage then in some cases it was missing the Message altogether. Finally I thought of using a dummy subclassed form and sending the message to to form. It's working now, but it seems like a mess to me. I'm pretty sure there's a better way. Any suggestions? thanks. P "It was when I found out I could make mistakes that I knew I was on to something." -Ornette Coleman

          R Offline
          R Offline
          Ryan Binns
          wrote on last edited by
          #4

          PostThreadMessage()/PeekMessage() should work. Messages can not get lost if they're being sent to the correct thread, as long as you specify NULL (or 0 in VB) for the HWND parameter, and PM_REMOVE as the flags parameter. My VB is a bit rusty, but:

          If PeekMessage(msg, 0, 0, 0, PM_REMOVE) = True Then
          If msg.message = MY_MESSAGE Then
          ExitProcess 0 ' Exit the process
          End If
          End If

          Where msg is the MSG structure variable and MY_MESSAGE is the value of the message that you are sending. I can't remember if the PM_REMOVE constant is declared like that, or whether it has a different name. Of course, if a different message is received, then it will need to be processed as well. If PeekMessage() returns False, then a message did not exist. Hope this helps, Ryan Being little and getting pushed around by big guys all my life I guess I compensate by pushing electrons and holes around. What a bully I am, but I do enjoy making subatomic particles hop at my bidding - Roger Wright (2nd April 2003, The Lounge)
          Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late - John Nichol "Point Of Impact"

          P 1 Reply Last reply
          0
          • R Ryan Binns

            PostThreadMessage()/PeekMessage() should work. Messages can not get lost if they're being sent to the correct thread, as long as you specify NULL (or 0 in VB) for the HWND parameter, and PM_REMOVE as the flags parameter. My VB is a bit rusty, but:

            If PeekMessage(msg, 0, 0, 0, PM_REMOVE) = True Then
            If msg.message = MY_MESSAGE Then
            ExitProcess 0 ' Exit the process
            End If
            End If

            Where msg is the MSG structure variable and MY_MESSAGE is the value of the message that you are sending. I can't remember if the PM_REMOVE constant is declared like that, or whether it has a different name. Of course, if a different message is received, then it will need to be processed as well. If PeekMessage() returns False, then a message did not exist. Hope this helps, Ryan Being little and getting pushed around by big guys all my life I guess I compensate by pushing electrons and holes around. What a bully I am, but I do enjoy making subatomic particles hop at my bidding - Roger Wright (2nd April 2003, The Lounge)
            Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late - John Nichol "Point Of Impact"

            P Offline
            P Offline
            parths
            wrote on last edited by
            #5

            Thanks a lot. That's exactly what I had done. But PeekMessage doesn't wait for the next message to arrive. As a result, if the message arrives when the program might be in the middle of a transaction. Could it be that by the time I call PeekMessage again the Message get processed (maybe by some default message handler which cannot comprehend the meaning of my user defined message?) "It was when I found out I could make mistakes that I knew I was on to something." -Ornette Coleman

            R 1 Reply Last reply
            0
            • P parths

              Thanks a lot. That's exactly what I had done. But PeekMessage doesn't wait for the next message to arrive. As a result, if the message arrives when the program might be in the middle of a transaction. Could it be that by the time I call PeekMessage again the Message get processed (maybe by some default message handler which cannot comprehend the meaning of my user defined message?) "It was when I found out I could make mistakes that I knew I was on to something." -Ornette Coleman

              R Offline
              R Offline
              Ryan Binns
              wrote on last edited by
              #6

              parths wrote: But PeekMessage doesn't wait for the next message to arrive. That's correct. If you need this, then you need to use GetMessage(). parths wrote: Could it be that by the time I call PeekMessage again the Message get processed (maybe by some default message handler which cannot comprehend the meaning of my user defined message?) Absolutely. If you're using more than one message loop, then you have to make sure all the message loops know about the message. It will probably be easier to code your main message loop similar to this:

              Do While GetMessage(msg, 0, 0, 0) = True
              If msg.message = MY_MESSAGE Then
              ExitProcess 0
              Else
              TranslateMessage msg
              DispatchMessage msg
              End If
              Loop

              This should work (I think ;) ) Ryan Being little and getting pushed around by big guys all my life I guess I compensate by pushing electrons and holes around. What a bully I am, but I do enjoy making subatomic particles hop at my bidding - Roger Wright (2nd April 2003, The Lounge)
              Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late - John Nichol "Point Of Impact"

              P 1 Reply Last reply
              0
              • R Ryan Binns

                parths wrote: But PeekMessage doesn't wait for the next message to arrive. That's correct. If you need this, then you need to use GetMessage(). parths wrote: Could it be that by the time I call PeekMessage again the Message get processed (maybe by some default message handler which cannot comprehend the meaning of my user defined message?) Absolutely. If you're using more than one message loop, then you have to make sure all the message loops know about the message. It will probably be easier to code your main message loop similar to this:

                Do While GetMessage(msg, 0, 0, 0) = True
                If msg.message = MY_MESSAGE Then
                ExitProcess 0
                Else
                TranslateMessage msg
                DispatchMessage msg
                End If
                Loop

                This should work (I think ;) ) Ryan Being little and getting pushed around by big guys all my life I guess I compensate by pushing electrons and holes around. What a bully I am, but I do enjoy making subatomic particles hop at my bidding - Roger Wright (2nd April 2003, The Lounge)
                Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late - John Nichol "Point Of Impact"

                P Offline
                P Offline
                parths
                wrote on last edited by
                #7

                Thanks, I had tried GetMessage, but then it doesn't return till it gets a message (any message) (right?), and that's a problem too because then I can't continue processing until I get a message. So I'll have to keep sending dummy messages to the app. I'll look for possible 'hidden' Message handlers, but is it possible that VB has it's own message handler which sort of peeks at all the messages coming into the app before forwarding them to the program message handlers or individual message handlers? I'm guessing that is what calls the VB event handler functions "It was when I found out I could make mistakes that I knew I was on to something." -Ornette Coleman

                R 1 Reply Last reply
                0
                • P parths

                  Thanks, I had tried GetMessage, but then it doesn't return till it gets a message (any message) (right?), and that's a problem too because then I can't continue processing until I get a message. So I'll have to keep sending dummy messages to the app. I'll look for possible 'hidden' Message handlers, but is it possible that VB has it's own message handler which sort of peeks at all the messages coming into the app before forwarding them to the program message handlers or individual message handlers? I'm guessing that is what calls the VB event handler functions "It was when I found out I could make mistakes that I knew I was on to something." -Ornette Coleman

                  R Offline
                  R Offline
                  Ryan Binns
                  wrote on last edited by
                  #8

                  parths wrote: I had tried GetMessage, but then it doesn't return till it gets a message (any message) (right?), and that's a problem too because then I can't continue processing until I get a message. So I'll have to keep sending dummy messages to the app. Not necessarily. Modify the message loop so it uses PeekMessage() instead of GetMessage() and do the processing after the PeekMessage() handling:

                  Do While True
                  If PeekMessage(msg, 0, 0, 0, PM_REMOVE) = True Then
                  If msg.message = MY_MESSAGE Then
                  ExitProcess 0
                  Else
                  TranslateMessage msg
                  DispatchMessage msg
                  End If
                  End If
                  ' Do your processing here
                  Loop

                  Ryan Being little and getting pushed around by big guys all my life I guess I compensate by pushing electrons and holes around. What a bully I am, but I do enjoy making subatomic particles hop at my bidding - Roger Wright (2nd April 2003, The Lounge)
                  Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late - John Nichol "Point Of Impact"

                  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