473,326 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 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 6871
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.