VB.Net Process.Start Property

I followed the advice from ID:10963463.  I also read up on msdn's webpage on the process.start overload:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdiagnosticsprocessclassstarttopic3.asp

So I attached the code to a button figuring I can call up the same window if I keep pushing the same button (my objective).  Unfortunately, every time I push the button, I create another instance of notepad (not what I wanted).  Anyways, here's the code:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim p As Process
        p = Process.Start("notepad.exe")
        p.WaitForInputIdle()
    End Sub

Thanks in advance.

WhyDidn'tItWork?
WhyDidntItWorkAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
If I understand your requirement correctly, then you would want to do something like below.

Note, I used a lambda function to search for the running proc. If you are using strictly .NET 2.0, then you would need to use a foreach/for loop instead.
Imports System.Diagnostics
Imports System.Runtime.InteropServices

Public Class Form1
    Private _notepadHnd As IntPtr

    <DllImport("user32.dll")> _
    Shared Function SetForegroundWindow(ByVal hWnd As IntPtr) As Boolean

    End Function
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim procs() As Process = Process.GetProcessesByName("notepad")
        Dim runningProc As Process = procs.FirstOrDefault(Function(proc As Process) proc.MainWindowHandle = Me._notepadHnd)

        If Me._notepadHnd = IntPtr.Zero OrElse runningProc Is Nothing Then
            Dim p As Process = Process.Start("notepad.exe")

            Me._notepadHnd = p.MainWindowHandle
        End If

        SetForegroundWindow(Me._notepadHnd)
    End Sub
End Class

Open in new window

0
WhyDidntItWorkAuthor Commented:
Hi.

I should have mentioned that I am using Visual Studios 2010 with Vista operating system.

Nope.  I tried out the code and every time I click the button, I still generate another Notepad instance.  All I want to do is click the button and have that application (notepad - which is already open) set as the active window.

Thanks for trying.
0
käµfm³d 👽Commented:
It seems there is some discrepancy between the format of the argument to GetProcessesByName(). Try modifying line 12 to this:
Dim procs() As Process = Process.GetProcesses()

Open in new window

0
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Try:

    FocusApp("notepad")

With:
Private Const SW_SHOWNORMAL As Integer = 1

    Private Declare Function IsIconic Lib "user32" (ByVal handle As Integer) As Integer
    Private Declare Function ShowWindow Lib "user32" (ByVal handle As IntPtr, ByVal nCmdShow As Integer) As Integer
    Private Declare Function SetForegroundWindow Lib "user32" (ByVal handle As IntPtr) As Integer

    Private Sub FocusApp(ByVal ExeName As String)
        Dim processes() As Process = Process.GetProcessesByName(ExeName)
        If processes.Length = 0 Then
            Process.Start(ExeName)
        Else
            ' Restore the window if it is minimized...
            If IsIconic(processes(0).MainWindowHandle) <> 0 Then
                ShowWindow(processes(0).MainWindowHandle, SW_SHOWNORMAL)
            End If

            ' Make it the foreground window...
            SetForegroundWindow(processes(0).MainWindowHandle)
        End If
    End Sub

Open in new window

0
WhyDidntItWorkAuthor Commented:
Nope. Sorry. Neither proposed solutions worked. kaufmed keeps generating multiple instances of the same app. idle mind's suggestion didn't pull up the ap.
0
käµfm³d 👽Commented:
Unfortunately, I don't have a Vista machine to test with. The closest I have is Win 7, but that's at the house. I guess that puts me out of the game. It's all up to you now Idle  : )
0
käµfm³d 👽Commented:
Hmmm... One more trick up my sleeve  ; )

I forgot to include this in my next-to-previous post. The reason I believe my code is "failing" on your machine (and mine at one point) is because the code launching notepad is getting past the Me._notepadHnd = p.MainWindowHandle line prior to the actual WindowHandle being created, which means that the line:
If Me._notepadHnd = IntPtr.Zero OrElse runningProc Is Nothing Then

Open in new window


will succeed on the first condition because the line:
Me._notepadHnd = p.MainWindowHandle

Open in new window


is assigning a value of zero, due to the handle not being available yet--of course, I could be talking out of my rear here. The mod I had implemented (but forgot to include in the post), was to wait for the input to be idle, via WaitForInputIdle(). The reason why I believe this works is that this method is dependent on a message loop in the target process, and if you've got a message loop, then you should have a window handle.

One last try. Here is the complete code I was running for my previous post. If this isn't it, then I'm tapping out  : )
Imports System.Diagnostics
Imports System.Runtime.InteropServices

Public Class Form1
    Private _notepadHnd As IntPtr

    <DllImport("user32.dll")> _
    Shared Function SetForegroundWindow(ByVal hWnd As IntPtr) As Boolean
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim procs() As Process = Process.GetProcesses()
        Dim runningProc As Process = procs.FirstOrDefault(Function(proc As Process) proc.MainWindowHandle = Me._notepadHnd)

        If Me._notepadHnd = IntPtr.Zero OrElse runningProc Is Nothing Then
            Dim p As Process = Process.Start("notepad.exe")

            p.WaitForInputIdle(-1)
            Me._notepadHnd = p.MainWindowHandle
        End If

        SetForegroundWindow(Me._notepadHnd)
    End Sub
End Class

Open in new window

0
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
I'm running Win 7.

Here's the whole thing that works on my system:  
Public Class Form1

    Private Const SW_SHOWNORMAL As Integer = 1

    Private Declare Function IsIconic Lib "user32" (ByVal handle As Integer) As Integer
    Private Declare Function ShowWindow Lib "user32" (ByVal handle As IntPtr, ByVal nCmdShow As Integer) As Integer
    Private Declare Function SetForegroundWindow Lib "user32" (ByVal handle As IntPtr) As Integer

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        FocusApp("notepad")
    End Sub

    Private Sub FocusApp(ByVal ExeName As String)
        Dim processes() As Process = Process.GetProcessesByName(ExeName)
        If processes.Length = 0 Then
            Process.Start(ExeName)
        Else
            ' Restore the window if it is minimized...
            If IsIconic(processes(0).MainWindowHandle) <> 0 Then
                ShowWindow(processes(0).MainWindowHandle, SW_SHOWNORMAL)
            End If

            ' Make it the foreground window...
            SetForegroundWindow(processes(0).MainWindowHandle)
        End If
    End Sub

End Class

Open in new window


Are you actually using Notepad in your code?  If not, what is the app?  Show us your code...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WhyDidntItWorkAuthor Commented:
It worked.  Thanks Idle Mind.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.