Windows message pump problems

Discussion in 'Python' started by Cantankerous Old Git, Aug 16, 2005.

  1. I am trying to write a program that I hope to get working as a
    command-line app to start with, and then eventually use a windows
    service wrapper to call it as a service. Its purpose is to attach
    to an already running (not ours) service using an API DLL, where
    it will do houskeeping and monitoring tasks.

    This is where it all gets strange. Although I can make calls into
    the API and do things proactively, when I register a callback for
    event notification, this callback doesn't get called. I am told
    that if I want event notifications, I must create a window and
    run a "message pump" on it, and pass the window handle to the API
    when I register my callback function. This is despite the fact
    that I don't want my service to ever display a window. I gather I
    don't have to act on any messages either, but somehow the API
    won't tell me about any events unless I do this.

    So I am trying to create a skeleton that will create a window and
    run a "Message Pump". This is fairly succesful, in that I can
    create the window and the API then magically calls my event handler.

    But I want to be able to stop my service on demand, and this
    involved killing the message pump loop and destroying the window.
    I can't figure out how to do that.

    Problem 1:
    If I have another thread call DestroyWindow after a delay, it
    gets an error "permission denied". I really can't see why.

    Problem 2:
    I thought that maybe catching some kind of message might help. I
    have tried registering both a message-map and a wndProc handler.
    In both instances (see the code below) I don't get the WM_CREATE
    message that I hope to see first. Also, if I use the wndProc
    method, the CPU slams to 100% and I seem to receive an infinite
    number of messages of some sort.

    Problem 3:
    I thought if I pump the messages using GetMessage and
    DispatchMessage in my own loop, I might be able to check a flag
    and exit the loop when required. But I can't figure out the
    callling arguments to these calls.


    Below is the code I have done so far. I have stripped to as
    simple as I can make it. I would really appreciate some guidance
    or links to articles. I am struggling to understand even the
    basics, I think.

    The Cog.



    import win32gui
    import win32api
    import win32con
    import traceback, time, threading

    def onCreate():
    print 'GOT WM_CREATE'

    def onDestroy():
    print 'GOT WM_DESTROY'

    def wndProc(hwnd, msg, wparam, lparam):
    #print '~~~wndProc get a message'
    if msg == win32con.WM_CREATE:
    onCreate()
    if msg == win32con.WM_DESTROY:
    onDestroy()
    return 0

    def registerWindowClass():
    wc = win32gui.WNDCLASS()
    wc.hInstance = hinst
    wc.lpszClassName = "Eric the half-a-bee"
    messageMap = {win32con.WM_CREATE : onCreate ,
    win32con.WM_DESTROY : onDestroy }
    wc.lpfnWndProc = messageMap # no messages if I do this
    #wc.lpfnWndProc = wndProc # infinite stream of
    messages if I do this
    return win32gui.RegisterClass(wc)


    def createWindow(registeredClassAtom):
    return win32gui.CreateWindow(
    registeredClassAtom, "This is a window",
    0, 0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT,
    0, 0, hinst, None)


    hinst = win32api.GetModuleHandle(None)

    hwnd = createWindow(registerWindowClass())

    win32gui.ShowWindow(hwnd, True) # I'll try hide it later
    win32gui.UpdateWindow(hwnd)


    # wait 10 secs then kill the window
    def timeKill():
    print 'sleeping...'
    time.sleep(10)
    print 'killing'
    win32gui.DestroyWindow(hwnd)


    print 'starting killer timer'
    threading.Thread(target=timeKill).start()

    # This message pump traps the thread and never returns
    win32gui.PumpMessages()
     
    Cantankerous Old Git, Aug 16, 2005
    #1
    1. Advertising

  2. Cantankerous Old Git

    Neil Hodgson Guest

    Cantankerous Old Git:

    > Problem 1:
    > If I have another thread call DestroyWindow after a delay, it gets an
    > error "permission denied". I really can't see why.


    http://msdn.microsoft.com/library/d...owreference/windowfunctions/destroywindow.asp

    """A thread cannot use DestroyWindow to destroy a window created by a
    different thread."""

    Posting WM_CLOSE to the window will probably work. If not, then send
    an application defined message to the window and handle it by calling
    DestroyWindow.

    Neil
     
    Neil Hodgson, Aug 17, 2005
    #2
    1. Advertising

  3. Neil Hodgson wrote:
    > Cantankerous Old Git:
    >
    >> Problem 1:
    >> If I have another thread call DestroyWindow after a delay, it gets an
    >> error "permission denied". I really can't see why.

    >
    >
    > http://msdn.microsoft.com/library/d...owreference/windowfunctions/destroywindow.asp
    >
    >
    > """A thread cannot use DestroyWindow to destroy a window created by a
    > different thread."""
    >
    > Posting WM_CLOSE to the window will probably work. If not, then send
    > an application defined message to the window and handle it by calling
    > DestroyWindow.
    >
    > Neil


    Sorry for the delay in answering. That put me on the right track,
    thanks. I post a custom message, and the handler calls DestroyWindow.

    The Cog
     
    Cantankerous Old Git, Aug 18, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    63
    Views:
    14,870
    christhevet
    Mar 20, 2011
  2. Replies:
    1
    Views:
    399
    John Jay Smith
    Jul 13, 2006
  3. Joe (GKF)

    OT - The pump's fixed.

    Joe (GKF), Nov 21, 2006, in forum: HTML
    Replies:
    8
    Views:
    332
    Joe (GKF)
    Nov 24, 2006
  4. George2

    COM message pump

    George2, Mar 27, 2008, in forum: C Programming
    Replies:
    0
    Views:
    372
    George2
    Mar 27, 2008
  5. George2

    message pump code for COM STA

    George2, Apr 11, 2008, in forum: C Programming
    Replies:
    0
    Views:
    407
    George2
    Apr 11, 2008
Loading...

Share This Page