Last few weeks I have worked on some project, which needed to use SendKeys to do something with NAV client. During developing this module I hade problem, how to synchronize of calling SendKeys with behaviour of the NAV client. You know, you send keys to the NAV, client is opening some window (form), it took some time, and as result the keys are sent to incorrect window. I tried to use Timer to synchronize it (wait for opening the window etc.), but it didn't work. The keys still ended in queue of another window that I wanted.
Once, I added BEEP(440,100) command into the timer when I sent the keys to know, when the event was fired and when the keys were sent into queue. And I wonder - the keys ended in correct window! I found out, that when you send some key sequence, which is opening e.g. File Save dialog, you need to wait for the dialog to open to send e.g. keys to "write" the file name into this dialog. And BEEP will help you with that. Because BEEP needs to talk with Operating system, it somehow "give" the control to OS. When you connect this with Timer event, you can do this:
1) Set timer (e.g. each 100ms), enable it
2) When the OnTimer is called first time, send first key sequence, which open the window
3) Call BEEP after sending the keys
4) In next call of OnTimer send second key sequence
5) Call BEEP after sending the keys
6) repat the steps through all windows and keys you want to send.
7) In last OnTimer, disable the timer
Because you used the BEEP command, each new key sequence will end in correct window, because the beep will be called right after all the keys are processed and NAV is free for processing next command. If you use this without BEEP, all keys will be send "to fast" and will be processed in incorrect windows (may be yes, may be no...).
And if you do not want to listen the beep sound, just set the frequency to 1Hz (0Hz is not working, it just skip the command). I found out, that time of the BEEP must be over 10ms, else it will not the effect. But it seems that the value can vary between computers.
I know, that you will be able now to do the cool things with NAV (e.g. automatic version controling :-)) now, when you know how to use BEEP command to synchronize SendKeys with client.
Take this as result of my research. I do not know exactly why the beep is working like this, but you must know, that IT IS WORKING (for me and my collegues)!
(tested on version 4.00SP3 and 5.0)
Have a fun with your new powerfull command BEEP!
Another quest... do you tried "SLEEP" command before that?
Of course, I tried it... but this is giving control to other programs and working only for 16bit... it means doing something other than I needed. I needed to give control to NAV during processing some code, and this is the main problem, because the "single" (I know, that it is not only one thread in real) threaded application...
Just wondering, if you tried a dusty old "YIELD" command before that?
You can google for SendKey as well (MSDN on-line help).
I'd be very interested in some sample code as well if possible (email@example.com)