How do I delay the execution of a command

Posted on 2004-11-28
Last Modified: 2011-09-20
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?
Question by:nmonroe
    LVL 18

    Assisted Solution

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

    LVL 28

    Accepted Solution

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

    Author Comment

    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.

    Author Comment

    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.

    Author Comment

    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!
    LVL 18

    Expert Comment

    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


    Author Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now