Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Appactivate within a DO LOOP using vb.net 2005

Posted on 2009-04-02
3
Medium Priority
?
578 Views
Last Modified: 2013-11-26
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

0
Comment
Question by:planetzed
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 24049917
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
0
 

Accepted Solution

by:
planetzed earned 0 total points
ID: 24057469
Hi,

Thanks for your response. I've managed to solve the problem in a much simpler way (for me anyway) by simply replacing the sendkeys.send with sendkeys.sendwait. I do have another question though in that I can't find how that If I close IE it won't error out. In .vbs I was able to use the following code ;

Set WShell = CreateObject("WScript.Shell")

Do
WinFound = WShell.AppActivate(" - Windows Internet Explorer")

If (WinFound) Then

Wshell.SendKeys "^{TAB}"
Wscript.Sleep (10 * 1000)

End If

Loop Until Not WinFound

As WinFound controls the whole process, I am then able to stop the loop once IE is no longer an active process. I can't find how to make this happen in Visual Studio. Any ideas?
0

Featured Post

Automating Terraform w Jenkins & AWS CodeCommit

How to configure Jenkins and CodeCommit to allow users to easily create and destroy infrastructure using Terraform code.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

719 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