Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1123
  • Last Modified:

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:
            AppActivate("MyProgramName")
            Me.txtMyTextBox.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?
0
nmonroe
Asked:
nmonroe
  • 4
  • 2
2 Solutions
 
armoghanCommented:
Hi nmonroe,
see the following artilce
http://support.microsoft.com/default.aspx?scid=kb;en-us;305369

Cheers!
0
 
iboutchkineCommented:
Try Application.DoEvents and/or Sleep instead of your message box
0
 
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:
            AppActivate("MyProgramName")
            Me.txtMyTextBox.Focus()

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.
thanks!
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
nmonroeAuthor Commented:
I tried to use
Application.DoEvents()
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
            '...wait
        Loop
    End Function

Nice, I pass 1 second to nSeconds and it works every time.
0
 
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!
0
 
armoghanCommented:
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
AppActivate("MyProgramName")
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


0
 
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.
-Nick
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now