Solved

How to tell if running in debugger

Posted on 2004-04-08
8
174 Views
Last Modified: 2010-04-05
Is there a compiler directive that I can use to omit certain code if I am running the application from the IDE.

specifically I have a splash screen that I would like to turn off when debugging.

something like

{$IF (not running in the IDE)}

    frmSplash := TfrmSplash.Create(nil);
    frmSplash.Show;
    frmSplash.Repaint;
    Sleep(3500);
{$ENDIF}
0
Comment
Question by:rbohac
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10784779
function RunningFromIDE: boolean;
var
IdeHnd: THandle;
MyApp,Text: STring;
Begin
 MyApp := ExtractFileName(Application.ExeName);
 Delete(MyApp,pos('.',MyApp),4);
 ShowMessage(MyApp);
 IdeHnd := FindWindow('TAppBuilder', nil);
   If IDEhnd > 0 then
       begin
        SetLength (Text, 100);
        GetWindowText (IdeHnd, PChar (Text), 100);
        If pos(MyApp+' [Running]',Text) > 0 then
           Result := True
        else
           Result := False;
   end else result := False;
end;


procedure TForm1.FormActivate(Sender: TObject);
begin
 if RunningFromIDE then
  ShowMessage('Yes');
 esle
 ShowMessage('No');
end;

Shane
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 10784824

The compiler define option is just that; it defines at compile time what is included/excluded from the source. (your in the IDE when compiling it, right...). So at compile time, the compiler has no idea if you plan on running the program standalone or in debug mode (throught the ide).

If you want to go with a define, you can set one in the project options / Directories-Conditionals tab, but you will still need to remember to remove it when compiling a "release" version.

Or,if running on an NT based system, you could also use the following function to skip certain blocks of code.

if not(IsDebuggerPresent) then
begin

  .... do splashscreen

end;

---------

Regards,
Russell

0
 
LVL 22

Accepted Solution

by:
Ferruccio Accalai earned 50 total points
ID: 10784904
you can use DebugHook: if its value > 0 then it's running from IDE

so

If DebugHook = 0 then begin
    frmSplash := TfrmSplash.Create(nil);
    frmSplash.Show;
    frmSplash.Repaint;
    Sleep(3500);
end;

0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 10784944

Nice one ;-)
I didn't realize that flag was there in the System unit.

Russell
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 11

Expert Comment

by:shaneholmes
ID: 10784984
Me either - <SMILE>

Shane
0
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 10784994
Well russell, i discovered that not so much time ago...Sometimes Delphi give's new surprises :))))
0
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 10785186
BTW i'd create the Splash Form without using Sleep and Repaint....

I'd use this method in your case:

program Project1;

uses
  Forms,

  Unit1 in 'Unit1.pas' {Form1},
  Unit2 in 'Unit2.pas' {Form2};

{$R *.res}

begin
  Application.Initialize;
  If DebugHook = 0 then  Begin
  Form2 := TForm2.Create(Application);
  Form2.Show;
  Form2.Update;
  end;
  Application.CreateForm(TForm1, Form1);
  If DebugHook = 0 then  begin
  Form2.Hide;
  Form2.Free;
  end;
  Application.Run;
end.
0
 
LVL 6

Author Comment

by:rbohac
ID: 10785516
Wow. Thats a nice trick.  Russell, I guess that makes sense about it already being compiled.

I use the sleep because the application loads rather fast, and I want to display the splash screen slightly longer.

I have that code in a try...finally which looks like

try
  Application.Initialize;
  {Splash Screen}
    if DebugHook = 0 then  {Only run when not in the debugger}
      begin
       frmSplash := TfrmSplash.Create(nil);
       frmSplash.Show;
       frmSplash.Repaint;
       Sleep(3500);
      end;

Application.CreateForm(s)....

finally
  if(frmSplash <> nil) then
    FreeAndNil(frmSplash);
  Application.Run;
end;
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
JSON on ISAPI REST Server using Delphi 2 210
Simple Delphi Question 9 77
code issue 8 85
Magic Software info 18 105
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now