How do I delay the execution of a command

I am launching picture and fax viewer using the shell command.  My VB app loses focus when this happens, so I use the following commands to regain focus:
This works only when I delay the code by placing a msgbox("") before the AppActivate function.  It appears that picture and fax viewer is stealing focus AFTER my commands execute.  The msgbox delays the execution enough, so that by the time I click OK, the focus is correct.  I do not even need the AppActivate command, just Me.txtMyTextBox.Focus() works.
Any suggestions?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi nmonroe,
see the following artilce;en-us;305369

Try Application.DoEvents and/or Sleep instead of your message box

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nmonroeAuthor Commented:
I have found both comments helpful, but I am not there yet.  I was using the following code:
    Private Function OpenInViewer(ByRef FileFullPath As String)
        Dim LaunchPictureFaxVar As String = "rundll32.exe C:\WINDOWS\System32\shimgvw.dll,ImageView_Fullscreen " 'shell code
        Shell(LaunchPictureFaxVar & FileFullPath) ' shell launch
    End Function

Now I am using the following code in a function thanks to armoghan
    Private Function OpenInViewer(ByRef FileFullPath As String) As String
        Dim CurrentProcessID As String
        Dim pInfo As New ProcessStartInfo 'Create a new ProcessStartInfo structure.
        pInfo.FileName = FileFullPath
        pInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized
        Dim p As Process = Process.Start(pInfo) 'Start the process.
        p.WaitForInputIdle() 'Wait for the process window to complete loading.
        Return p.ToString 'if process name is needed
    End Function

I then have the following code to switch back to my VB App in the main code:

The problem with the new code is that it only switches to my app once.  When I view multiple files, one after another, it does not switch back.  I even manually close the viewer in-between loops in case that matters.  Any thoughts?  I am going to try a timer function next.
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

nmonroeAuthor Commented:
I tried to use
This did not work.  I think I am using it in the wrong way.

I then tried this:
    Private Function waitTimer(ByRef nSeconds As Double)
        Dim finish, start As Double
        start = Microsoft.VisualBasic.DateAndTime.Timer
        finish = start + nSeconds   ' Set duration
        Do While Microsoft.VisualBasic.DateAndTime.Timer < finish
    End Function

Nice, I pass 1 second to nSeconds and it works every time.
nmonroeAuthor Commented:
I used both answers to solve the problem.  Though not as perfect as I would like, It works for my needs.  I am worried that if the file is large, and it takes more than a second to launch, I might have problems.

Since I posted more code, and fleshed out the purpose more, I will bump value to 200 and wait a few days for a better solution.  If I do not hear back, I will split between both contributors.  Thanks again!
I could not fully understood, but I think you are talking about more than one instnace of one application and then finding the right application
so instead of dong
do this to find an array of different instances of MyProgramName
  Dim p As Process() = System.Diagnostics.Process.GetProcessesByName("MyProgramName")

and then switch to the right one using
Activate = SetForegroundWindow(p(0).MainWindowHandle())

NOTE: SetForegroundWindow is a Win32 function to focus any Window

nmonroeAuthor Commented:
I have been testing the Microsoft.VisualBasic.DateAndTime.Timer code from above.  Set at .5 seconds it works great.  I am going with that.  Thanks for help from both of you, I learned much.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.