Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 834293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:39:03+00:00 2026-05-15T04:39:03+00:00

I have a very frustrating bug in an application I am working on. The

  • 0

I have a very frustrating bug in an application I am working on. The routine is supposed to do something in one window, and then return focus to the other at the end of the method, but when I started to use a large data set the other day, the focus stopped returning at the end. I stepped through the code one line at a time, and the errors stopped. so, i figure its a timing issue of some kind. I trace through until i find what i suspect is the culprit. A call to ShellExecute(…), that terminates an image editor i use.
(http://msdn.microsoft.com/en-us/library/bb762153(VS.85).aspx)

Now, if I step past this call, and then continue to run the program, everything works fine, but if I just run past this line, the error occurs. how can this be? I have a call to SetFocus() at the very end of this method. shouldnt the program hit this no matter what?

This is all so very frustrating…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T04:39:03+00:00Added an answer on May 15, 2026 at 4:39 am

    First thing that should be clear is that Win32 API calls that are related to windows/messages/focus and etc. do not depend on timing.
    Every thread has its own window/messaging subsystem, there’s no race conditions here.

    What you describe is something else. You actually launch another process (application), which runs concurrently with yours.
    Note that ShellExecute is an asynchronous function. It returns immediately after creating the process, and from now on your application and the process you’ve created run concurrently.

    Now, since only one window in the system may have a focus at a time – it’s very likely that the process you’ve created just steals the focus from you.
    In order to avoid this – you should first wait for that process to finish working, and only then restore the focus to your window and continue working.

    For this you have to obtain the handle of the created process, and call a Win32 waiting function on it. ShellExecute doesn’t return you the handle of the created process. However ShellExecuteEx – does.
    BTW it also allows you to launch a process with instruction for it not to show the UI, if this is what you want.

    You should write it like this:

    SHELLEXECUTEINFO sei;
    memset(&sei, 0, sizeof(sei));
    sei.cbSize = sizeof(sei);
    sei.fMask = SEE_MASK_NOCLOSEPROCESS;
    sei.lpFile = L"notepad.exe";
    sei.nShow = SW_SHOWNORMAL; // or SW_HIDE if you don't want to show it
    
    if (ShellExecuteEx(&sei))
    {
        // wait for completion
        WaitForSingleObject(sei.hProcess, INFINITE);
        CloseHandle(sei.hProcess);
    }
    

    This should be helpful

    P.S. Of course you should close the handle of the created process. That is, CloseHandle must be called after WaitForSingleObject.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have very simple window where I have 2 buttons - one for cancel,
Very frustrating one... I have tried many combinations of ', and so on but
I'm having a very frustrating problem with my javascript files. I have one global
I'm running into a very frustrating bug. I have a java class that reads
Ok, I have a very frustrating problem. I am parsing a webpage and need
I have very little experience working with sockets and multithreaded programming so to learn
I am having a very frustrating problem with Microsoft Visual Web Developer. I have
This should be very simple, but its very frustrating. I have a LinqToSql as
I have a very frustrating problem which I cannot result. Its just not adding
I have a very frustrating problem: The $_POST values disappears when using pagination, I've

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.