Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27554

VS 2008 Proccess open, activate, close

$
0
0
Hello,
I have one scenario with opening, activating and closing a project which don't work.

Here are two declarations on form level:
Code:

    Dim explorerID As Integer = 0
    Dim eProc As Process = Nothing

Then with menu item I started a proccess:
Code:

        If explorerID > 0 Then ' if i have previous proccess ID
            Try
                AppActivate(explorerID) ' try to activate
            Catch
                eProc.Dispose()  ' if can't activate a proccess dispose it
                eProc = Nothing  ' and set proccessID to 0
                explorerID = 0
            End Try
        End If

        If explorerID = 0 Then ' if I havent proccess ID (or it is cleared by previous code)
            eProc = New Process 'start a new proccess
            With eProc.StartInfo
                .FileName = "explorer.exe"
                .Arguments = myPath
                .WindowStyle = ProcessWindowStyle.Normal
            End With
            eProc.Start()
            explorerID = eProc.Id 'take a new proccess ID
        End If

And in the FormClosing event handler...
Code:

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        KillExplorer() 'kill procces before exit if it exists
    End Sub

    Private Sub KillExplorer()
        If explorerID > 0 Then ' if my proccess is opened
            Try
                  eProc.Dispose()  ' try to stop it
                  eProc = Nothing
                  Process.GetProcessById(explorerID).Kill() ' try to kill it
            Catch
            End Try
        End If
        explorerID = 0 ' and reset proccess ID
    End Sub

Idea is to prevent to open explorer window more than once.
And to close it after usage or at least at exit.

As I can see:
HTML Code:

    Process.GetProcessById(explorerID).Kill()
    AppActivate(explorerID)

 ...don't work as expected or at all.

Please any help to get this working.

Viewing all articles
Browse latest Browse all 27554

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>