Link to home
Start Free TrialLog in
Avatar of planetzed
planetzed

asked on

Appactivate within a DO LOOP using vb.net 2005

Hi,

I'm trying to add a System Tray icon  that will bring focus to Internet Explorer 7 and tab between all the windows within it. I'm writing this in VB using Visual Studio 2005. My knowledge of both VB and Visual Studio are limited. I've added a 'NotifyIcon' to a form. I don't actually need the form but this is only way I can see do add the notifyicon. I'm then basically activating the IE window and then using a sendkey to tab between the windows. I'm also using a sleep command. This is placed within  a simple DO LOOP. My problems are that when I double click the system tray icon my mouse and keyboard freeze. The only thing I can do is press ctrl-alt-del and this allows me to stop the process running. I know that is is caused by the do loop. The other problem is the sleep command. It won't neccesarily tab between the windows at the time period that I set. So if I set the sleep for 2 seconds, It will sometimes quickly move between the tabs and other times it will take many more seconds.
Also, if I run this as a simple .vbs file it works perfectly.
I've attached the code. Please help!
Imports System
Imports System.Windows.Forms
Imports Microsoft.VisualBasic.Interaction
Imports System.Threading
 
Public Class Form1
 
    Private Sub IE_Tab_MouseDoubleClick_1(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles IE_Tab.MouseDoubleClick
        Me.Hide()
        Dim WinFound As String
 
        WinFound = (" - Windows Internet Explorer")
 
        Do
            AppActivate(WinFound)
            SendKeys.Send("^{TAB}")
            Thread.Sleep(2000)
 
        Loop
 
    End Sub
End Class

Open in new window

Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

You need a "responsive" Sleep():

    Private Sub Delay(ByVal DelayInSeconds As Integer)
        Dim ts As TimeSpan
        Dim targetTime As DateTime = DateTime.Now.AddSeconds(DelayInSeconds)
        Do
            ts = targetTime.Subtract(DateTime.Now)
            Application.DoEvents() ' keep app responsive
            System.Threading.Thread.Sleep(50) ' reduce CPU usage
        Loop While ts.TotalSeconds > 0
    End Sub

Example:

    Delay(10) ' hold for ten seconds
ASKER CERTIFIED SOLUTION
Avatar of planetzed
planetzed

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial