472,780 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,780 software developers and data experts.

Windows message pump problems

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()

Aug 16 '05 #1
2 6793
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/de...troywindow.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
Aug 17 '05 #2
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/de...troywindow.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
Aug 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Brian Keating EI9FXB | last post by:
Hello all, Wonder what approach is used for this problem. I have a MDIApplication, the MDIClinets are to be in a seperate thread. So I've done something like this, // Create a new Show...
8
by: Brian Keating EI9FXB | last post by:
Would I be correct in saying that the only way to get a user message into a Windows form would be to use P/Invoke with Message? Of is there some part of the .NET API that I am totally un aware...
11
by: DW | last post by:
I've gotten this question a couple of times in interviews and I don't know what they are looking for: How do you update a control's property, such as a textbox.text property, from a thread, in...
14
by: | last post by:
Hi All, I am little confused here, hope you can help me. While processing WM_POWERBROADCAST (wParam=PBT_APMQUERYSUSPEND), I MUST to do some lengthy operation(30 sec) before system Suspends or...
11
by: Ajith Menon | last post by:
I have created a windows application in which the form needs to be resized on the MouseMove event. The windows resize function takes a lot of CPU cycles. And as the resize function is called on the...
1
by: igor | last post by:
I recently attempted to incorporate an OCX into a .net windows service using .net framework 1.1 The ocx failed to perform as expected, meaning the OCX events failed to execute. I specifed the...
2
by: =?Utf-8?B?Sm9obiBG?= | last post by:
Hello All, I have a question about using an ActiveX control with a Windows Servce in C#. I believe it is not possible to properly setup an ActiveX control in a windows service as the particular...
28
by: | last post by:
I have a multi threaded windows form application that runs great after calling Application.Run(). Application.Run is required for a COM component I a using in the app (required for message loop). ...
41
by: pbd22 | last post by:
Hi. I know my windows service works when i run it in debug mode on my dev machine. It also works in release mode on my dev machine. But, when I move the service to a production server, it...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.