🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.delphi.nativeapi
Article #247

Problem with message dispatching in thread

#247
From: Magnus Oskarsson
Date: Mon, 26 Jan 2004 13:03
35 lines
2189 bytes
I have a thread that spends most of its time waiting for various events
and/or input in its message queue. Support for the latter was added so that
it would be possible to create objects such as timers in code executed in
this thread. It turns out that the thread receives a number of messages even
when no objects such as timers are created, and that dispatching these
messages occasionally causes an access violation. The messages in these
cases were WM_DEVICECHANGE with wParam 7 and lParam 0. The key code looks
like this:

PeekMessage(Msg, 0, 0, 0, PM_NOREMOVE); {initialize the message queue}
...
SignaledObject := MsgWaitForMultipleObjects(EventCount, HandleArray, False,
  INFINITE, QS_ALLINPUT);
case SignaledObject of
  ...
  {cases for various signaled synchronization objects}
  ...
  else {input in the message queue}
    while PeekMessage(Msg, 0, 0, 0, PM_REMOVE) do
      DispatchMessage(Msg);
end;

What is it that is wrong with this code? I have tried a fix that replaces
the DispatchMessage row with:

      if OneOfMyExpectedMessages(Msg.message) then
        DispatchMessage(Msg);

where the added function returns True only for a few messages, e.g.
WM_TIMER. This fix removes the AVs, but I would like to know if I introduce
any new problems by not dispatching all messages.

Regards

Magnus Oskarsson
Technical Details
Message-ID: <40151062$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!fu-berlin.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f94dd,a9e8600237d4d3c8,start
X-Google-Attributes: gidf94dd,public
X-Google-ArrivalTime: 2004-01-26 05:10:19 PST
From: "Magnus Oskarsson" <magnus.oskarsson@enera.se>
Newsgroups: borland.public.delphi.nativeapi
Subject: Problem with message dispatching in thread
Date: Mon, 26 Jan 2004 14:03:48 +0100
Lines: 38
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.50.4922.1500
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4925.2800
NNTP-Posting-Host: 62.95.73.89
Message-ID: <40151062$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1075122274 62.95.73.89 (26 Jan 2004 05:04:34 -0700)
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!fu-berlin.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail