ShowMessage in Service halts process

I created a service (i.e. runs as a service, not an application).  I found afterwards it was calling a piece of code which had a showmessage contained in it.  Consequently it was halting continuation.

Short of ensuring my code does not contain any showmessages, are there any alternatives?  A lot of this code is used in win applications.

Who is Participating?
markschenkelConnect With a Mentor Author Commented:
<<If you set the service to not interact with the desktop, this should stop the messages popping up and pausing the services running.>>
I experimented with this setting.  When it is unchecked the service simply hangs (I guess it is probably displaying a message on the desktop btframework alludes to).  But when checked it displays the showmessage on the desktop (and of course halts the app).

I have seen similar like solutions to Geert_Gruwez.  What I was hoping for was a solution which would detect any showmessages at compile time.  Is it possible to detect if ShowMessage is compiled??? (assuming if it is not used the optimizer removes it)

Ultimately I guess this rests with cautious/careful coding practices.  
pmunroConnect With a Mentor Commented:
If you set the service to not interact with the desktop, this should stop the messages popping up and pausing the services running.
btframeworkConnect With a Mentor Commented:
By default windows NT (4.0, 2000, XP, Vista) has "two" desktops. One is for user and other one is system. You may see the system desktop before login.
So any service in the system by default uses system desktop. You should switch it to user desktop before showing any messages, dialogs or something like that.
Any way, take a look at this please:
Geert GConnect With a Mentor Oracle dbaCommented:
create a unit with a ShowMessage procedure/function which you let it call Dialogs when in an ordinary app or logs the message to file when in a service application

unit AppMessenger;
uses Dialogs, SvcMgr;
  ServiceAppName = 'ServiceApp';
  fServiceApp: integer = 0;
  fEventLogger: TEventLogger = nil;
function IsServiceApp: boolean;
  if fServiceApp = 0 then
    if Assigned(SvcMgr.Application) then
      fServiceApp := 2;
      fEventLogger := TEventLogger.Create(ServiceAppName);
    end else
      FServiceApp := 1;
  Result := fServiceApp = 2;
procedure ShowMessage(const Msg: string);
  if IsServiceApp then

Open in new window

Geert GOracle dbaCommented:
it will allways be compiled when you use the dialogs unit
the trick is to fool delphi to use your own unit by placing it last in the uses list
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.