Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6357555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:11:18+00:00 2026-05-24T23:11:18+00:00

I want to start a potentially long running background process from Delphi. I want

  • 0

I want to start a potentially long running background process from Delphi. I want to leave the process running independently, but first I want to check that the process started OK.

If anything went wrong on startup, I want to capture any output written to standardErr and log it. If the background process starts OK, my program needs to be able to exit and leave the spawned process running.

The psuedo code would be something like this:

process:=RunProgramInBackground('someCommand.exe');
sleep(1000); // Wait a bit to see if the program started OK
if process.Finished and process.ExitCode=FAIL then 
    Raise Exception.Create(process.ErrorStream);
process.Dispose; // Close any connection we may still have to the running process
Program.Exit; // Background process keeps running

I’ve looked at a few things (WinExec, CreateProcess, ShellExecute, JclMiscel) but can’t find any examples for what I’m trying to do. What is the best way to do this?

I’m using Delphi 2010

The background process is a 3rd party program I don’t have the source to.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T23:11:20+00:00Added an answer on May 24, 2026 at 11:11 pm

    I ended up using the example No’Am linked to, and adding code to check that the process started OK. This function only checks the exit code of the background process, it doesn’t read the StdErr output.

    Here is what I did:

    /// <summary> Runs a new process in the background. Waits for a short period, then checks that the process started succesfully.
    /// If the process has already finished, checks the exit status. Otherwise, leaves it to run. </summary>
    /// <param> ProgramName The executable name, including any parameters.</param>
    /// <param> TimeOut Milliseconds to wait before checking the process has executed correctly</param>
    /// <param> Directory The full path of the working directory</param>
    /// <exception> Exception If the process was not started correctly or if the process was started but returned
    /// an error before the timeout.</exception>
    procedure ExecBackgroundProcess(ProgramName : String; TimeOut: Integer; Directory:string);
    var
        StartInfo : TStartupInfo;
        ProcInfo : TProcessInformation;
        CreateOK : Boolean;
        status: Cardinal;
        theExitCode: Cardinal;
    begin
        FillChar(StartInfo,SizeOf(TStartupInfo),#0);
        FillChar(ProcInfo,SizeOf(TProcessInformation),#0);
        StartInfo.cb := SizeOf(TStartupInfo);
        UniqueString(ProgramName); // Required if a const string is passed in. Otherwise the program crashes.
        CreateOK := CreateProcess(nil, PChar(ProgramName), nil, nil,False,
                  CREATE_NEW_PROCESS_GROUP+NORMAL_PRIORITY_CLASS+CREATE_NO_WINDOW,
                  nil, PChar(Directory), StartInfo, ProcInfo);
    
        if CreateOK then
        begin
            status:=WaitForSingleObject(ProcInfo.hProcess, TimeOut);
            if status<> WAIT_TIMEOUT then
            begin
                // Program has exited. Get exit code.
                GetExitCodeProcess(ProcInfo.hProcess, theExitCode);
                if theExitCode<>0 then raise Exception.Create('Program '''+ProgramName+''' failed with exit code '+IntToStr(theExitCode));
            end
        end
        else
          Raise Exception.Create('Unable to run '+ProgramName+' in directory '+Directory);
    
        CloseHandle(ProcInfo.hProcess);
        CloseHandle(ProcInfo.hThread);
    end;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to start a background process in a Java EE (OC4J 10) environment.
I want to start nutch from Java. How can I start cygwin from a
Many poeple have online startups in their head that may potentially attracts millions, but
I have a situation like this: start(); <Some code> end(); I want start() function
I want to start coding in Python or Ruby. Since I own a Lego
I want to start an application which will use ajax push, however the web
I want to start a new thread using a C function, not an objective-C
I want to start a number of subprocesses in my Python script and then
I want to start web developing with java. I don't know how to start,
I want to start development using qt-embedded on my embedded device project. Can you

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.