Deleted

G'd evening Isaac,
That's endless, as soon as you open IE it will close and open it. If you want to stop the cycle. just disable the IEchase at the end of the first round.
G'd luck
 
Hello Everyone,
I am creating a piece of software that detects if Internet Explorer is open, then restarts it...
The problem is that, when I start the timer(IEChase) that calls the sub IEKillandStart() which then calls IEStart(), it starts opening Internet Explorer endlessly!

VB.NET:
    Private Sub IEChase_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IEChase.Tick
        'IF I.E. IS OPEN, THEN CLOSE IT. 
        For Each selProcess As Process In Process.GetProcesses
            'IF IE HAS STARTED
            If selProcess.ProcessName = "iexplore" Then
                'START THE I.E. RESTARTER 
                IEKillandStart()
                IEChase.Stop()
            End If
        Next
    End Sub

Private Sub IEKillandStart()
        'IF I.E. IS OPEN
        For Each selProcess As Process In Process.GetProcesses
            If selProcess.ProcessName = "iexplore" Then
                'THEN CLOSE IT
                Dim pProcess() As Process = System.Diagnostics.Process.GetProcessesByName("iexplore")
                For Each p As Process In pProcess
                    p.Kill()                    
                    For Each strtProcess As Process In Process.GetProcesses
                        If Not strtProcess.ProcessName = "iexplore" Then
                            'IF IT IS CLOSED, THEN START IT...
                            IEStart()                        
                        End If
                    Next
                Next
            End If
        Next
    End Sub

   Private Sub IEStart()
        'OPEN INTERNET EXPLORER AND GO TO THE DEFAULT PAGE 
        Process.Start("iexplore", "about:blank")
    End Sub
Isaac, what exactly are you wanting to do by re-starting each instance of IE? What's the purpose?

As for your loops there, here's a few things wrong that I see off the bat:
Private Sub IEChase_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IEChase.Tick
    'IF I.E. IS OPEN, THEN CLOSE IT. 
    For Each selProcess As Process In Process.GetProcesses
        'You are looping all of the processes, which is fine
        'IF IE HAS STARTED
        If selProcess.ProcessName = "iexplore" Then
            'You are checking if the process is IE or not by name, which is fine
            'START THE I.E. RESTARTER 
            'You already have the IE process, why call another sub to loop the processes again?
            'Why not just call selProcess.CloseMainWindow() right here instead of that sub that does 3 loops (which is my guess to why you've got the infinite opening of IE going on)
            IEKillandStart()
            IEChase.Stop()
        End If
    Next
End Sub

Private Sub IEKillandStart()
        'IF I.E. IS OPEN
        'So you loop through all the processes again... didn't you just do that??
        For Each selProcess As Process In Process.GetProcesses
            'Again, checking for only the IE ones.. just did that right before this sub
            If selProcess.ProcessName = "iexplore" Then
                'THEN CLOSE IT
                'You already have the process in selProcess, why are you getting it again????
                Dim pProcess() As Process = System.Diagnostics.Process.GetProcessesByName("iexplore")
                
                'A third loop.. for what you already have
                For Each p As Process In pProcess
                    'Now you finally decide on one of the 3 copies of this process to kill it
                    p.Kill()                    
                    For Each strtProcess As Process In Process.GetProcesses
                        'A 4th loop, this one makes less sense than the other 2
                        If Not strtProcess.ProcessName = "iexplore" Then
                            'So now if this process is NOT internet explorer, you're going to start a new instance of IE
                            'I really have no idea what you're wanting to do here, but I'm sure this isn't it.
                            'IF IT IS CLOSED, THEN START IT...
                            IEStart()                        
                        End If
                    Next
                Next
            End If
        Next
    End Sub

   Private Sub IEStart()
        'OPEN INTERNET EXPLORER AND GO TO THE DEFAULT PAGE 
        
        'This opens a blank window, not their default page
        Process.Start("iexplore", "about:blank")
    End Sub

Personally here's how I would do it:
Private Sub IEChase_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IEChase.Tick
    Dim PrcList() As Process = Process.GetProcessesByName("iexplore")
    If PrcList.Length > 0I Then
        'Stop the timer
        IEChase.Stop()
        
        'Loop through an array of only the IE processes
        For Each prc As Process In Process.GetProcessesByName("iexplore")
            'Tell the process to close gracefully rather than just killing it
            prc.CloseMainWindow()
        Next
        
        'Open a single new instance
        Process.Start("iexplore")
    End If
End Sub
 
Hi JuggaloBrotha, I would like to start by thanking you for replying so quickly!
I would also like to thank you for fixing my code! :) Your code has really saved me lots of time & effort!

My code may seem a bit messy, but I am somewhat new to VB.net -- I am learning how to program by myself using internet forums like this one to make my software..

About the below software:
I am creating a small program for my school that when it detects the word "facebook" in the URL, it restarts Internet Explorer...
(After the code you sent me, I have fixed up my whole project (seen below). It works better than ever!)

On the form:
One text box (txtURL.text)- I could also make it a label...

If you see something that you think could be changed or improved, tell me! Anything to get my program up-and-going is greatly appreciated!

So, here is the code:
Imports System.Net
Imports System.IO
Imports SHDocVw
Imports mshtml


Public Class UrlBlocker


    Private Sub URLBlocker_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        MainCtrl.Start()
    End Sub


    Private Sub MainCtrl_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MainCtrl.Tick
        Dim PrcList() As Process = Process.GetProcessesByName("iexplore")
        'IF IE HAS STARTED
        For Each prc As Process In Process.GetProcessesByName("iexplore")
            'THEN READ URL
            URLReader.Start()
            MainCtrl.Stop()
        Next
    End Sub


    Private Sub URLReader_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles URLReader.Tick
        Dim SWs As New SHDocVw.ShellWindows
        For Each IE As InternetExplorer In SWs
            'MAKE THE TEXT IN OUR TEXTBOX EQUAL THAT OF THE URL IN INTERNET EXPLORER (I.E.)
            txtURL.Text = IE.LocationURL()
            AlltoLowerCase()
            URLReader.Stop()
        Next
    End Sub


    Private Sub AlltoLowerCase()
        txtURL.Text = txtURL.Text.ToLower
        'START THE URL Filter
        TextFilter.Start()
    End Sub


    Private Sub TextFilter_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextFilter.Tick
        'IF txtURL CONTAINS THE WORD 'FACEBOOK' THEN
        If txtURL.Text.Contains("facebook") Then
            'RESTART I.E.
            txtURL.Text = ""
            IEChase.Start()
            TextFilter.Stop()
        Else
            'IF IT DOESN'T, THEN CONTINUE AS NORMAL
            MainCtrl.Start()
            TextFilter.Stop()
        End If
    End Sub


    Private Sub IEChase_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IEChase.Tick
        Dim PrcList() As Process = Process.GetProcessesByName("iexplore")
        If PrcList.Length > 0I Then
            IEChase.Stop()


            'Loop through an array of only the IE processes
            For Each prc As Process In Process.GetProcessesByName("iexplore")
                'CLOSE I.E. - GRACEFULLY...
                prc.CloseMainWindow()
            Next

            'OPEN I.E.
            Process.Start("iexplore", "http://www.google.com")
            IEChase.Stop()
            MainCtrl.Start()
        End If
    End Sub  
End Class
Well I'm glad I could help, but if this is all just for blocking a few websites (like Facebook) then why is facebook.com not blocked at the school's firewall? Having a program running on each of the computers to prevent this (for internet explorer only) is a really stupid way to handle the accepting/blocking of websites by IP or URL, which is what a network firewall rule is for. That way all computers on the network (Windows, Linux, Mac) and all Web browsers (IE, FF, Chrome, Safari, Operah) doesn't matter, the firewall rejects all of those packets regardless.
 
Hmm, looks like the https stuff cannot be filtered like http can, because of it's security, which means I don't have any suggestions because I thought if you put in a firewall rule being disallow "facebook.com" that it would block both http and https since that's the point of blocking a domain like that. Keep in mind there are proxy websites, like this one facebookfirewall.com, that were made to get around firewall blocked sites.

Also, you might want to have your program specifically look for "facebook.com" rather than just "facebook" because students might be doing a social media research people and while they would not be allowed to go to facebook.com, who's to say that they wont be wanting to load a page that simply has facebook in the name (after the .com of course)? You'd be blocking legit sites.
And then there's those proxy websites, which means you might want a way to maintain a list of dis-allowed sites and your program simply gets that list from somewhere and all you have to do is edit that list without re-compiling and re-distributing the application for every list change.
 
Yes, that's along the lines of how I would do it too. Except I would load the list on app start up, into a string collection: List(Of String) so the app doesn't download the file each time that timer runs, but I would have a 2nd timer set to run every couple of hours to refresh that list, the 2nd timer would make sure the checking timer is stopped, reload the list, then restart that checking timer again.
 
I was thinking a little more along the lines of this:
    Private m_UrlList As New List(Of String)
    Private m_UrlFile As String = "http://dl.dropbox.com/u/********/Textfile.txt"

    Private Sub LoadUrlList()
        'Clear the list
        m_UrlList.Clear()

        'Declare the WebClient
        Dim UrlFileClient As New WebClient

        Try
            'Get the url file contents
            Dim UrlListString As String = UrlFileClient.DownloadString(m_UrlFile)

            'Add the urls to the string collection, splitting on the carrage return
            m_UrlList.AddRange(UrlListString.Split(New String() {Environment.NewLine}, StringSplitOptions.None))
        Catch ex As Exception
            MessageBox.Show(String.Format("Error downloading file{0}{1}", Environment.NewLine, ex.ToString), "LoadUrlList")
        Finally
            UrlFileClient.Dispose()
        End Try
    End Sub

    Private Sub LoadUrlListTimer_Tick(sender As Object, e As System.EventArgs) Handles LoadUrlListTimer.Tick
        'Stop the IE checking timer
        IEChase.Stop()

        'Load the URL list
        Call LoadUrlList()

        'Restart the IE checking timer
        IEChase.Start()
    End Sub

    Private Sub IEChase_Tick(sender As Object, e As System.EventArgs) Handles IEChase.Tick
        Dim PrcList() As Process = Process.GetProcessesByName("iexplore")
        If PrcList.Length > 0I Then
            'Stop the timer
            IEChase.Stop()

            'Loop through an array of only the IE processes
            For Each prc As Process In Process.GetProcessesByName("iexplore")
                Dim CurrentURL As String = "<URL From the IE tab>"
                'Do your thing to get the URL's from the IE tabs and when you have one you can loop the m_UrlList like this
                For Each Str As String In m_UrlList
                    If Str = CurrentURL Then
                        'Tell the process to close gracefully rather than just killing it
                        prc.CloseMainWindow()
                    End If
                Next Str
            Next prc
        End If
    End Sub
 
Back
Top