How would I reference a process that is already started?

GingerCode

New member
Joined
Aug 14, 2014
Messages
4
Programming Experience
Beginner
I have an application I am working on where I would like to take all of the items of my list box and pass them into a URL and launch it in internet explorer. I have it all working except one small detail. Everytime I run my code it opens a new window instead of a new tab in internet explorer. Obviously it is because I am using process.start multiple times in my for loop.

Any ideas please?


VB.NET:
Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim testbool As Boolean = True


        For i = 0 To ListBox1.Items.Count - 1
            ListBox1.SetSelected(i, True)
            Dim urlstring As String
            urlstring = "https://www.google.com/search?q=" & ListBox1.SelectedItem & "&oq=test&aqs=chrome..69i57j69i60j0l2j69i59j0.658j0j9&sourceid=chrome&es_sm=93&ie=UTF-8"""
            Process.Start("IExplore.exe", urlstring)
            Threading.Thread.Sleep(5000)
            If testbool = False Then
                SendKeys.Send("^(t)")
            End If
            testbool = False
        Next
    End Sub


    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        For Each s As String In Me.TextBox1.Lines
            Dim nextLineText As String = s
            ListBox1.Items.Insert(0, s)
        Next
        TextBox1.Focus()
        TextBox1.Text = String.Empty
    End Sub


    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        TextBox1.Multiline = True
    End Sub
End Class
 
And what would you do with it even if you could "reference" that existing process? There's no method you can call on the Process object that will help you. It's just a matter of how the browser is implemented. If you do as you're doing now except you let the default browser open the URLs and that default browser happens to be Firefox, it will open the pages in tabs in one window because that's how Firefox is implemented. IE is not. You could potentially get into using the IE object model and control it that way but that's going to be a hassle.

Why are you specifying IE rather than accepting the default browser anyway? Is there any good reason? I know I'm not the only person who gets annoyed when applications insist on opening a page in a specific browser rather than respecting my decision about what browser I prefer to use. There can sometimes be a good reason, like a site is specifically built to work better in a particular browser. Such sites are thankfully rare these days though and you're only doing a Google search anyway, so why do you need to specify IE?
 
Thank you jmcilhinney, my code works great in firefox! I was using a google search as a test. I work at a helpdesk and occasionally I have to open several tickets at once, and manually opening all of those tabs can get very annoying... So this is my solution.
 
GingerCode said:
Everytime I run my code it opens a new window instead of a new tab in internet explorer. Obviously it is because I am using process.start multiple times in my for loop.
VB.NET:
Process.Start("IExplore.exe", urlstring)
GingerCode said:
my code works great in firefox!
Your code does not relate to Firefox and is instead forcefully starting new IE processes.

Process.Start(url) will open the url in default browser. It will start a new process if default browser is not currently running, and otherwise add a new tab to existing process of default browser. I tested this with current versions of IE, Firefox and Chrome set as default browser, and they all behaved like this.
 
I tested this with current versions of IE, Firefox and Chrome set as default browser, and they all behaved like this.

Ah, it didn't occur to me before but the reason that it would have been creating multiple windows for IE would have been that each call to Process.Start would be executed before the first call got the window open. If you wait for the window to open on the first call then each subsequent call will add a tab to that window instead of creating a new window. I must have already had Firefox open when I tested but not IE. I'll post a code example after a proper test.
 
Hmmm... I can certainly confirm what JohnH said for IE11 and Firefox: if there is already a browser window open then repeated calls to Process.Start will open each URL in a new tab in that window, otherwise each URL will ne opened in a new window. The bad news is that I can't see any easy way to ensure that a window is open before making the second and subsequent calls to Process.Start.

My hope was that Process.Start would return a Process object if a new window was opened and Nothing otherwise. As it turns out, it returns a Process object regardless for Firefox and Nothing regardless for IE. That means that using Process.WaitForInputIdle or Process.MainWindowHandle are not options. Using Process.GetProcessesByName would require you to know what the default browser was in order to know what process name to look for, so that's not trivial.

A possible hack is to call Process.Start once and then call Thread.Sleep to wait for a prescribed amount of time. That will allow the first window to open, if there isn't one already, before opening the rest of the URLs. It will add an unwanted delay between the first and second tab if a window is already open though. There's also the issue that, if opening the window is delayed, you might still get multiple windows.
Private Sub OpenUrls(ParamArray urls As String())
    If urls.Length > 0 Then
        Process.Start(urls(0))
    End If

    If urls.Length > 1 Then
        Thread.Sleep(1000)

        For i = 1 To urls.GetUpperBound(0)
            Process.Start(urls(i))
        Next
    End If
End Sub
 
Hmmm... I can certainly confirm what JohnH said for IE11 and Firefox: if there is already a browser window open then repeated calls to Process.Start will open each URL in a new tab in that window, otherwise each URL will ne opened in a new window. The bad news is that I can't see any easy way to ensure that a window is open before making the second and subsequent calls to Process.Start.
I can confirm the problem of opening multiple urls (at the same time) when there was no existing browser window for both IE and Firefox, Chrome seems to handle it well regardless. The hack of waiting a second was also a valid workaround in these cases for me.
 

Latest posts

Back
Top