Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using a THandle

Status
Not open for further replies.

minckle

Programmer
Mar 17, 2004
142
GB
Im trying to create an app which allows user to import txt files into a MySQL db.

Im using a THandle to run a MySQL routine called mysqlimport. and then i want to run a query to select the imported data.

If i step through my app everything runs fine. The data is imported and the refresh works ok. but if i let the app run normal speed it doesnt work. its as though it trys running the refresh code before the the import code has finish. Can this type of thing happen???? is there any way to stop the refrdh code running until the import code has stopped?????

Below is my current code.

ProgramHandle := THandle
cmdFile := String;
// cmdFile = import command
ProgramHandle := WinExec(PChar(cmdFile),SW_Hide);
query1.close;
query1.open;


Thanks
 
its as though it trys running the refresh code before the the import code has finish. Can this type of thing happen

yes, that can happen. Another example, when printing a Word document from Delphi, if you don't wait long enough before trying to close the word document, you will get that annoying "Word is trying to print, closing will end all jobs"

I don't know the "best" way to handle the situation, but I will occasionally add a Sleep command:

Sleep(2000);

to allow the processing to have time enough to complete.

There may be a better way, but if so, I don't know it.

HTH

Leslie
 
Hi,

you must wait for the program to finish it's execution.
while Leslie's idea to use sleep is certainly valid, it's not the proper way to implement this. here's some ms info about starting processes :

here's some sample code.

use it like this in your program :
Code:
...
Exitcode:=ExecuteAndWait(cmdFile); 
if Exitcode <> 0 then Showmessage('procedure failed')
else
 begin
  query1.close;
  query1.open;
 end;
...

Here's the ExecuteAndWait function :

Code:
const ErrUINT = High(Cardinal);

function ExecuteAndWait(const CommandLine : string) : cardinal;
var
 tSI : TStartupInfo;
 tPI : TProcessInformation;
 dwI : DWORD;
 VarU : UINT;
begin
 Result := ErrUINT;
 FillChar(tSI, sizeof(TStartupInfo), 0);
 tSI.cb := sizeof(TStartupInfo);

 if (CreateProcess(nil, pchar(CommandLine), nil, nil, False, 0, nil, nil, tSI, tPI)) then begin
   dwI := WaitForSingleObject(tPI.hProcess, INFINITE);

   if (dwI = WAIT_OBJECT_0) then
     if (GetExitCodeProcess(tPI.hProcess, dwI)) then Result := dwI;

   CloseHandle(tPI.hProcess);
   CloseHandle(tPI.hThread);
 end;
end;

--------------------------------------
What You See Is What You Get
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top