How to start a process and then later stop it.

Category: visual studio vb

Question

Robert Homes on Mon, 02 Oct 2017 12:10:42


Can someone tell me what's wrong with this attempt?

Imports System
Imports System.Diagnostics
Imports System.ComponentModel

Module modProcess

    Class PolProcess
        Public Shared Sub StartProcess()
            Dim PolProcess As New Process()
            Try
                PolProcess.StartInfo.UseShellExecute = False
                ' You can start any process, HelloWorld is a do-nothing example.
                PolProcess.StartInfo.FileName = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86) & "\BriefCase\Pol\Pol.exe"
                PolProcess.Start()
                ' This code assumes the process you are starting will terminate itself. 
                ' or you can do it programmatically from this application using the Kill method.
            Catch e As Exception
                MsgBoxer(e.Message, MsgBoxStyle.OkOnly, "Pol", "Critical")
            End Try
        End Sub 'Main

        Public Shared Sub StopProcess()
            Dim PolProcess As Process
            Try
                PolProcess.Kill()
            Catch ex As Exception
                MsgBoxer(ex.Message, MsgBoxStyle.OkOnly, "Pol", "Critical")
            End Try
        End Sub

    End Class

End Module


Robert Homes

Replies

leshay on Mon, 02 Oct 2017 12:27:06


Hi

Try with something like this. Obviously I had to change things to suit my system so you would need to reset to your own. Your 'kill'process was not accessing the same as the 'start' process.

Option Strict On
Option Explicit On
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        PolProcess.StartProcess()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        PolProcess.StopProcess()
    End Sub
End Class
Class PolProcess
    Shared PolProcess As New Process()
    Public Shared Sub StartProcess()
        Try
            PolProcess.StartInfo.UseShellExecute = False
            ' You can start any process, HelloWorld is a do-nothing example.
            PolProcess.StartInfo.FileName = "C:\Windows\System32\notepad.exe"
            PolProcess.Start()
            ' This code assumes the process you are starting will terminate itself. 
            ' or you can do it programmatically from this application using the Kill method.
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Pol")
        End Try
    End Sub 'Main

    Public Shared Sub StopProcess()
        '      Dim PolProcess As Process
        Try
            PolProcess.Kill()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Pol")
        End Try
    End Sub

End Class

Robert Homes on Mon, 02 Oct 2017 14:19:45


I'm wanting to put the code into a module, instead of a form. This is what I've got with your help so far:

Imports System
Imports System.Diagnostics
Imports System.ComponentModel

Module modProcess

    Class PolProcess
        Shared PolProcess As New Process()
        Public Shared Sub StartProcess()
            Try
                PolProcess.StartInfo.UseShellExecute = False
                ' You can start any process, HelloWorld is a do-nothing example.
                PolProcess.StartInfo.FileName = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86) & "\BriefCase\Pol\Pol.exe"
                PolProcess.Start()
                ' This code assumes the process you are starting will terminate itself. 
                ' or you can do it programmatically from this application using the Kill method.
            Catch ex As Exception
                MsgBoxer(ex.Message, MsgBoxStyle.OkOnly, "Pol", "Critical")
            End Try
        End Sub 'Main

        Public Shared Sub StopProcess()
            Try
                PolProcess.Kill()
            Catch ex As Exception
                MsgBoxer(ex.Message, MsgBoxStyle.OkOnly, "Pol", "Critical")
            End Try
        End Sub

    End Class

End Module

The problem is that when I try to call the thing like this:

    Private Sub mnuDocPol_ItemClick(sender As Object, e As ItemClickEventArgs) Handles mnuDocPol.ItemClick
        PolProcess.StartProcess()
    End Sub

I get this message:

"The requested operation requires Elevation"

Paul P Clement IV on Mon, 02 Oct 2017 14:29:46


Different issue. UAC (User Account Control) is indicating that the app you are attempting to execute requires Administrator privileges.

leshay on Mon, 02 Oct 2017 14:39:17


Hi

The simple example I posted (whether in a module or not), does as expected. I don't have the file structure you are using (I believe Briefcase was in a windows system prior to my Windows 10). Accessing something in a protected system folder is not something you should be doing.

Perhaps, make a shortcut for the executable in a User folder somewhere and try using that.

BTW: what is

MsgBoxer

is it a global sub you have elsewhere?


Regards Les, Livingston, Scotland


Paul P Clement IV on Mon, 02 Oct 2017 14:42:03


I get this message:

"The requested operation requires Elevation"


Robert Homes

FYI:

http://www.vb-helper.com/howto_2008_launch_runas.html

Robert Homes on Mon, 02 Oct 2017 14:43:58


I am using DevExpress in my program(s), which has a component called "LookAndFeel", used for changing or setting the colors of all the forms and controls in the program. MsgBoxer is a form I created to mimic MsgBox a little, and it can use the LookAndFeel stuff, whereas MsgBox can't.

"BriefCase" is the name of the LLC company I use for my programs, not the Windows program.

The Pol.exe I am trying to execute with the process is in the ProgramFiles(x86) folder. Does that make it "protected"? I was able to run it simply with "Process.start("Pol.exe") - with the full pathname prepended, of course, without all this Process Class stuff. I wanted to use the Class stuff so I could gradually get several other programs to run from one program, and then be able to stop them. Can you stop a program you started with "Process.start(<program>) without using a class. How would you identify which process you wanted to "kill"?


Robert Homes


leshay on Mon, 02 Oct 2017 15:37:51


Hi

First off, thanks for clarifying the MsgBoxer query.

OK, it is nothing to do with the old Briefcase stuff, but is your own.

You shouldn't use any of the system folders for your programmes/data, but instead, use a User folder - such as

    Dim user1 As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\Movies"

    Dim user2 As String = My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData

Here is the some code that shows example of multiple start/stop processes. It is very basic and has little/no error checking, but may help. It uses a ComboBox to select which to start/stop.

' Form1 with ComboBox1
' Button1 - Start
' Button2 - Stop
Option Strict On
Option Explicit On
Public Class Form1
    Dim myApps As New Dictionary(Of String, MyApp)
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        myApps.Add("WordWise", New MyApp With {.Name = "WordWise", .StartFile = "C:\Users\lesha\Documents\Visual Studio 2017\Projects\WordWise\WordWise\bin\Release\WordWise.exe"})
        myApps.Add("Clock", New MyApp With {.Name = "Clock", .StartFile = "C:\Users\lesha\Documents\Visual Studio 2017\Projects\!!!MyWorking\Clock.exe"})

        With ComboBox1
            .DataSource = New BindingSource(myApps, Nothing)
            .DisplayMember = "key"
            .ValueMember = "value"
        End With
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim p As MyApp = CType(ComboBox1.SelectedValue, MyApp)
        If Not p.Running Then
            p.StartIt()
            p.Running = True
        End If
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim p As MyApp = CType(ComboBox1.SelectedValue, MyApp)
        If p.Running Then
            p.StopIt()
            p.Running = False
        End If
    End Sub

    Class MyApp
        Property Name As String = Nothing
        Property pro As New Process
        Property StartFile As String = Nothing
        Property Running As Boolean = False
        Sub New()
            pro.StartInfo.UseShellExecute = False
        End Sub
        Sub StartIt()
            If StartFile = Nothing Then Exit Sub
            pro.StartInfo.FileName = StartFile
            pro.Start()
        End Sub
        Sub StopIt()
            pro.Kill()
        End Sub
    End Class
End Class



Regards Les, Livingston, Scotland