?
Solved

ShellExecute() takes too long

Posted on 2007-07-31
7
Medium Priority
?
1,187 Views
Last Modified: 2013-11-20
I have a ShellExecute() call in an application that opens Firefox.exe:

            hdl = ShellExecute(
            NULL,            // parent window
            "open",            // verb: open = "double-click"
            "C:\\Program Files\\Mozilla Firefox\\firefox.exe",      
            NULL,            // no parameters passed to program being opened
            NULL,            // default directory
            SW_NORMAL);                  // flags: open
      
when a button on an MFC dialog box is selected.  The problem is that it takes about 30 seconds to open Firefox this way, while if I double click a shortcut on my desktop that's set to

                                            C:\Program Files\Mozilla Firefox\firefox.exe

Firefox opens right away.  What's taking ShellExecute() so long and how can I open Firefox faster from my application?

Thanks,
steva
0
Comment
Question by:steva
  • 3
  • 2
  • 2
7 Comments
 
LVL 22

Expert Comment

by:mahesh1402
ID: 19606394
>> how can I open Firefox faster from my application?

I just made a test and at my side it just open normally without any delay. However you may try to execute same using WinExec() or CreateProcess() API to check if that makes any difference to ensure if its just shellexecute issue.

e.g

WinExec("C:\\Program Files\\Mozilla Firefox\\firefox.exe",SW_SHOWNORMAL);

OR

STARTUPINFO si;
PROCESS_INFORMATION pi;

CreateProcess(  NULL,"C:\\Program Files\\Mozilla Firefox\\firefox.exe",NULL, NULL,FALSE,0,NULL,NULL,&si,&pi )
     

-MAHESH
0
 
LVL 22

Expert Comment

by:mahesh1402
ID: 19606425
just a NOTE :
You've got the last parameter of ShellExecute wrong - it should be "SW_SHOWNORMAL"  instead of SW_NORMAL however that should not cause any problems.

-MAHESH
0
 

Author Comment

by:steva
ID: 19606506
It looks like winuser.h defines both as 1:

/*
 * ShowWindow() Commands
 */
#define SW_HIDE             0
#define SW_SHOWNORMAL       1
#define SW_NORMAL           1
#define SW_SHOWMINIMIZED    2
#define SW_SHOWMAXIMIZED    3
#define SW_MAXIMIZE         3
#define SW_SHOWNOACTIVATE   4
#define SW_SHOW             5
#define SW_MINIMIZE         6
#define SW_SHOWMINNOACTIVE  7
#define SW_SHOWNA           8
#define SW_RESTORE          9
#define SW_SHOWDEFAULT      10
#define SW_FORCEMINIMIZE    11
#define SW_MAX              11

I'll try your other ideas tomorrow.  Thanks.
steva
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:jkr
ID: 19608770
>>The problem is that it takes about 30 seconds to open Firefox this way

If that behaviour is the same when doubleclicking on a HTML file, there is a configuration issue. You cannot compare that to starting FF directly.
0
 

Author Comment

by:steva
ID: 19613061
Double-clicking an html file opens up FireFox ok.  

I think the problem is the way I've programmed this. When the application I've written first runs it checks the license and if it's still temporary it brings up a Buy.exe program that offers a chance to buy a permanent license.  The application starts us Buy.exe with ShellExecuteEx, passing the number of days left on the temporary license. The application can't continue until Buy.exe returns - either by the user deciding to continue with the temporary license or to purchase a permanent license - so the application waits with

      do {
                  GetExitCodeProcess(ShellExecuteInfo.hProcess, &ExitCode); //  check status of Buy.exe
            }
            while(ExitCode == STILL_ACTIVE);

If you look at Task Manager you can see that 99% of the CPU time is spent in this apolication  loop while Buy.exe is running so I suspect that Buy.exe is only getting about 1 out of every 100 CPU cycles to go off and start FireFox, hence the delay.

Any suggestions on the correct way to code this?

Thanks,
steva
0
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 19613084
>>Any suggestions on the correct way to code this?

Yes, change that to

WaitForSingleObject(ShellExecuteInfo.hProcess,INFINITE);

This will wait at 0% CPU load.

BTW, you could also use

DWORD ExecuteAndWaitForCompletion   (   LPSTR   pszCmd)
{
   STARTUPINFO         si;
   PROCESS_INFORMATION pi;

   BOOL                bRes;

   DWORD               dwCode  =   0;

   MSG                           msg;

   ZeroMemory  (   &si,    sizeof  (   STARTUPINFO));

   si.cb           =   sizeof  (   STARTUPINFO);
   si.dwFlags      =   STARTF_USESHOWWINDOW;
   si.wShowWindow  =   SW_SHOWNORMAL;

   bRes    =   CreateProcess   (   NULL,
                                   pszCmd,
                                   NULL,
                                   NULL,
                                   TRUE,
                                   NORMAL_PRIORITY_CLASS,
                                   NULL,
                                   NULL,
                                   &si,
                                   &pi
                               );

   while   (   WAIT_OBJECT_0   !=  MsgWaitForMultipleObjects   (   1,
                                                                   &pi.hProcess,
                                                                   FALSE,
                                                                   INFINITE,
                                                                   QS_ALLINPUT
                                                               )
           )
           {
               while   (   PeekMessage (   &msg,   NULL,   0,  0,  PM_REMOVE))
                       {
                           DispatchMessage     (   &msg);
                       }
           }

   GetExitCodeProcess  (   pi.hProcess,    &dwCode);

   CloseHandle (   pi.hProcess);
   CloseHandle (   pi.hThread);

   return  (   dwCode);
}

which is quite a time-tested function here on EE.
0
 

Author Comment

by:steva
ID: 19613368
Perfect!  

       WaitForSingleObject(ShellExecuteInfo.hProcess,INFINITE);

works!  And I'll store away the other code in my toolbox.

Thanks.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Suggested Courses
Course of the Month17 days, 10 hours left to enroll

829 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