Solved

How to tell if running in debugger

Posted on 2004-04-08
8
176 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

777 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