Solved

waitforsingleobject.....

Posted on 1997-11-15
6
357 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now