简体   繁体   English

VB如何结束使用按钮运行的batchfile / cmd命令?

[英]VB How do I end a batchfile/cmd command running with a button?

This is the code i'm using: 这是我正在使用的代码:

Public Class Form1
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Process.Start("C:\batchfile.bat")
End Sub

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

I'd like to know how to make Button1_Click kill the "batchfile.bat?" 我想知道如何让Button1_Click杀死“batchfile.bat?” The batchfile is opening a CMD prompt, if that helps anyone in anyway. 批处理文件正在打开CMD提示符,如果这对任何人都有帮助。

You need to start the Process instance returned by Process.Start() in a field in your form. 您需要在表单的字段中启动Process.Start()返回的Process实例。

You can then call the Kill() method. 然后,您可以调用Kill()方法。

For example: 例如:

Public Class Form1
    Private mySubTask As Process

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        mySubTask = Process.Start("C:\batchfile.bat")
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        mySubTask.Kill()
    End Sub

You will need to figure out what to do if the user clicks start twice. 如果用户单击“开始”两次,您将需要确定要执行的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM