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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SetForegroundWindow() api function is not working sometimes -- why?

Status
Not open for further replies.

adesh

IS-IT--Management
Mar 5, 2002
2
SG

Hi,

My Application is doing the following:

a) Launch the Notepad.exe application.
b) Open the 'c:\abc.txt' text file.
- Focus the Notepad application{ used SetForegroundWindow() api function }.
- Used the sendkeys to open the 'c:\abc.txt' file{ used sendkeys() function }.

The Problem what i am facing is :

My application is working very well but it fails one time out of 10 times of execution,
the one time is failed because of Notepad application is not got focussed before sending
the sendkeys to open the text file.

You are appreciated if u can figure out this problem.
I am eagerly waiting for ur valuable solution.

Rgds
Adesh.
adesh@kmap.biz,adesh_y@yahoo.com
 
Loop until GetForegroundWindow() matches the destination window, or use SetWindowText (or SendMessage and WM_SETTEXT) to place the text without making it the foreground window.

Remedy
 
Use the WaitForInputIdle() API to wait for the Notepad instance to become idle.

....But if all you want to do is open a textfile with notepad, you should just set the file as a command parameter to the notepad instance you're spawning; there's no need for using sendkeys etc. to open the file.
Greetings,
Rick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top