title: End program Delphi
body: <p>I have a program that needs data from a SQL-Server, it can't work without (well, it can but has totally no use).</p>
  
  <p>There are 2 autocreate forms, DMOD and Main, in that order.</p>
  
  <p>This is the code in the OnCreate of DMOD:</p>
  
  <pre><code>  if not fileexists(UdlFile) then
    begin
      ITRCreateFile(UdlFile);
      ShellExecute(Application.Handle,'open',UdlFile,nil,nil,SW_SHOW);
       try
         cnConnect.Close;
         if gServerPort &lt;&gt; '' then
            cnConnect.connectionString:= 'Provider=SQLOLEDB.1;Password=*;Persist Security Info=True;User ID=itreflex;Initial Catalog=ExquisStudio;Data Source=' + gServerName + '\' + gServerPort + ';Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=' + gServerName + ';Use Encryption for Data=False;Tag with column collation when possible=False'
         else
            cnConnect.connectionString:= 'Provider=SQLOLEDB.1;Password=*;Persist Security Info=True;User ID=itreflex;Initial Catalog=ExquisStudio;Data Source=' + gServerName + ';Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=' + gServerName + ';Use Encryption for Data=False;Tag with column collation when possible=False';
         cnConnect.Open();
     except
        Showmessage('Problems with dataconnection - error SQL data');
        screen.Cursor := crDefault;
        Application.terminate;
     end;
    end
    else
    begin
       try
         cnConnect.Close;
         if gServerPort &lt;&gt; '' then
            cnConnect.connectionString:= 'Provider=SQLOLEDB.1;Password=*;Persist Security Info=True;User ID=itreflex;Initial Catalog=ExquisStudio;Data Source=' + gServerName + '\' + gServerPort + ';Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=' + gServerName + ';Use Encryption for Data=False;Tag with column collation when possible=False'
         else
            cnConnect.connectionString:= 'Provider=SQLOLEDB.1;Password=*;Persist Security Info=True;User ID=itreflex;Initial Catalog=ExquisStudio;Data Source=' + gServerName + ';Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=' + gServerName + ';Use Encryption for Data=False;Tag with column collation when possible=False';
          cnConnect.Open();
       except
         Showmessage('Problems with dataconnection - error SQL data');
         screen.Cursor := crDefault;
         Application.terminate;
       end;
    end;
  </code></pre>
  
  <p>The problem is, the program closes, there is nothing left that is visible, but the process keeps running, mostly at 95% or higher CPU-usage. That's not really good....</p>
  
  <p>I did some digging and found <a href="https://stackoverflow.com/questions/16142522/thread-not-terminated-while-application-is-terminated-under-delphi">Thread not terminated while application is terminated under Delphi</a></p>
  
  <p>I added <code>ExitProcess(0);</code> after both <code>Application.terminate;</code> and now the process closes as should be. I used 0 because the parameter is required, I have no idea what it really should be.</p>
  
  <p>My question is: Is it OK to do it this way? It does what I want but I have the feeling I'm overlooking something.</p>