Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to tell if running in debugger

Posted on 2004-04-08
8
Medium Priority
?
195 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 23

Accepted Solution

by:
Ferruccio Accalai earned 200 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
LVL 11

Expert Comment

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

Shane
0
 
LVL 23

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 23

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

670 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