如何启动进程然后再将其停止。 [英] How to start a process and then later stop it.

查看:59
本文介绍了如何启动进程然后再将其停止。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我这次尝试有什么问题吗?

进口系统
进口System.Diagnostics
进口System.ComponentModel

模块modProcess

类PolProcess
公共共享子StartProcess()
Dim PolProcess As New Process()
试试
PolProcess.StartInfo.UseShellExecute = False
'你可以启动任何进程,HelloWorld是一个无用的例子。
PolProcess.StartInfo.FileName = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86)& " \BriefCase\Pol\Pol.exe"
PolProcess.Start()
'此代码假定您启动的进程将自行终止。
'或者您可以使用Kill方法从此应用程序以编程方式执行此操作。
Catch e As Exception
MsgBoxer(e.Message,MsgBoxStyle.OkOnly," Pol","Critical")
结束尝试
结束子'主

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

结束类

结束模块




Robert Homes

解决方案

您好


尝试这样的事情。显然我必须改变一些东西以适应我的系统,所以你需要重置为自己的系统。你的'kill'进程没有像'start'进程那样访问。

 Option Strict On 
选项显式在
公共类Form1
私有子Form1_Load(发件人为对象,e为EventArgs)处理MyBase.Load
PolProcess.StartProcess()
结束Sub

Private Sub Button1_Click(sender As Object,e As EventArgs)Handles Button1.Click
PolProcess.StopProcess()
End Sub
End Class
Class PolProcess
共享PolProcess为新进程()
公共共享子StartProcess()
尝试
PolProcess.StartInfo.UseShellExecute = False
'你可以启动任何进程,HelloWorld是一个做 - 没什么例子。
PolProcess.StartInfo.FileName =" C:\ Windows \System32 \\\
otepad.exe"
PolProcess.Start()
'此代码假定您启动的进程将自行终止。
'或者您可以使用Kill方法从此应用程序以编程方式执行此操作。
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" )
结束尝试
结束次级

结束等级


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

解决方案

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


这篇关于如何启动进程然后再将其停止。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆