Utility Start/Stop Management Service
-
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
-
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
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
-
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
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
-
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
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 IfWhere 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" -
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 IfWhere 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"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
-
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
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
LoopThis 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" -
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
LoopThis 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"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
-
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
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 ofGetMessage()
and do the processing after thePeekMessage()
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
LoopRyan 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"