Solved

waitforsingleobject.....

Posted on 1997-11-15
6
363 Views
Last Modified: 2013-04-01
Hi!

I have a small problem. I use the API waitforsingleobject.
I makeing a program that runs MS Publisher 97 Setup prog. with some switches (/qt (means quietmode)). But wenn I start the program and press the button that starts the installtionprog. And the program is started. But it hangs wenn it should look for installed objects....

Anyone know what the problem is? It's Delphi or Publisher that bugs, or?

I use Delphi 2.0.....

TimeMan
0
Comment
Question by:TimeMan
  • 3
  • 2
6 Comments
 
LVL 1

Expert Comment

by:jackb022197
ID: 1350540
Have you checked whether or not the installation program also locks up when you run it seperately, and not from within your D2 application??
If it also locks up when you run it seperately, then it is not your delphi application...

0
 

Author Comment

by:TimeMan
ID: 1350541
Hi!

I have test that. It is not locking...

//TimeMan
0
 

Expert Comment

by:jgalang
ID: 1350542
Send me a snippet of your code and I'll tell you whats wrong with it.

Things to check:
1. Is your WaitForSingleObject set to time-out or not?
2. Are you using the CreateProcess to execute your program (and detecting if its done with WaitForSingleObject?)

0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

Author Comment

by:TimeMan
ID: 1350543
Hi!

Here it comes

TimeMan


function WinExecAndWait32(FileName:String; Visibility : integer):integer;
var
   zAppName: array[0..512] of char;
   zCurDir: array[0..255] of char;
   WorkDir: string;
   StartupInfo: TStartupInfo;
   ProcessInfo: TProcessInformation;
begin
   StrPCopy(zAppName,FileName);
   GetDir(0,WorkDir);
   StrPCopy(zCurDir,WorkDir);
   FillChar(StartupInfo,Sizeof(StartupInfo),#0);
   StartupInfo.cb := Sizeof(StartupInfo);
   StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
   StartupInfo.wShowWindow := Visibility;
   if not CreateProcess(nil,
      zAppName,    { pointer to command line string }
      nil,         { pointer to process security attributes }
      nil,         { pointer to thread security attributes }
      false,       { handle inheritance flag }
      CREATE_NEW_CONSOLE or  { creation flags }
      NORMAL_PRIORITY_CLASS,
      nil,         { pointer to new environment block }
      nil,         { pointer to current directory name }
      StartupInfo, { pointer to STARTUPINFO }
      ProcessInfo) then Result := -1 { pointer to PROCESS_INF }
   else
   begin
      WaitforSingleObject(ProcessInfo.hProcess, INFINITE);
      GetExitCodeProcess(ProcessInfo.hProcess, Result);
   end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
WinExecAndWait32 ('c:\temp\pub\setup.exe /qt',0);
form2.show;
end;
0
 

Accepted Solution

by:
jgalang earned 190 total points
ID: 1350544
Your app was doing okay, but it wasnt proceessing any messages (keyboard, screen refresh, etc) and the setup program was probably taking too long, so it seemed like your program was locked up (it is! but the setup program, running quietly wasnt)

your app locked up because of the following line:

>> WaitForSingleObject( ProcessInfo.hProcess, INFINITE );

I modified a portion of your code to make it a little bit more user friendly.

First, I added a local variable in your WinExecAndWait32 method

>>
>> var                     // additional variables
>>   dwWait : DWORD;       //
>>

Second, I modified the portion where you WaitForSingleObject so that app messages are processed while the object is signaled
(1000 means, it processes app messages every 1 second)

>>
>> repeat
>>   dwwait := WaitForSingleObject( ProcessInfo.hProcess, 1000 );
>>   GetExitCodeProcess(ProcessInfo.hProcess, Result);
>>   Application.ProcessMessages;
>> until dwwait <> WAIT_TIMEOUT;
>>

And last, If you don't want the user to be able to do anything else while your setup program is running (or to prevent them from doing anything stupid) you'll need to have something like this when invoking the WinExecAndWait32 method

>>
>> procedure TForm1.BitBtn1Click(Sender: TObject)
>> const
>>   bInWait: boolean = False;
>> var
>>   Result : Integer;
>> begin
>>   //  don't launch MYPROGRAM.EXE if we are already
>>   //  waiting for MYPROGRAM.EXE to  finish
>>   if bInWait then Exit;
>>   // notify ourself that we have launched MYPROGRAM.EXE
>>   bInWait := True;
>>   try
>>     // launch MYPROGRAM.EXE
>>     Result := WinExecAndWait32( 'C:\MYPROGRAM.EXE', 0 );
>>   finally
>>     case Result of
>>     // check Result for errors
>>     end;      
>>     // notify ourself that we're done messing around
>>     // with MYPROGRAM.EXE
>>     bInWait := False;
>>   end;
>> end;
>>

I tested the above modification and it worked perfectly, running different setup programs, as well as some time-critical apps I've written.

I hope this got to you in time to be useful.

TTYL.

jgalang
0
 

Author Comment

by:TimeMan
ID: 1350545
Hi!

I havent test it yet...but it seems to work better than mine...


//TimeMan
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi XE10, MySQL Query 4 143
add combobox item based on numbers 9 144
Delphi selector screen 2 80
how can i search if string exist in array ? 3 59
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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

803 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