【问题标题】:Find PID VB.net查找 PID VB.net
【发布时间】:2015-10-03 19:09:55
【问题描述】:

我正在尝试在我的游戏中制作服务器启动器。我必须使用不同的配置启动 GameServer.exe。
GameServer.exe GameServer1.cfg
GameServer.exe GameServer2.cfg
GameServer.exe GameServer3.cfg 等

这是我必须启动每个 .exe 和 .cfg 的当前代码

Private Sub GS1_Click(sender As Object, e As EventArgs) Handles GS1.Click
    Dim path As String = Directory.GetCurrentDirectory()
    Dim gameservercfg As String = GameServer1.Text
    Dim newpath As String = path + ("\")
    System.Diagnostics.Process.Start(newpath + "GameServer.exe ", gameservercfg)
End Sub

这是我通过进程名称重新启动它的代码。

Private Sub GSRES_Click(sender As Object, e As EventArgs) Handles GSRES.Click
    Dim path As String = Directory.GetCurrentDirectory()
    Dim gameservercfg As String = GameServer.Text
    Dim newpath As String = path + ("\")
    Dim p() As Process

    p = Process.GetProcessesByName("GameServer")
    If p.Count > 0 Then
        ' Process is running
        Process.GetProcessesByName("GameServer")(0).Kill()
        System.Diagnostics.Process.Start(newpath + "GameServer.exe ", gameservercfg)
    Else
        ' Process is not running
        MsgBox("GameServer is not running!")
    End If
End Sub

使用该代码,它会重新启动随机游戏服务器。
问题是,我如何通过 ProcessID 而不是 Process name 来做到这一点?

【问题讨论】:

  • Process.Start() 返回一个 Process 对象。将它(或 Id 属性本身)保存在变量或列表中,然后使用保存的进程 ID 来终止您想要使用 Process.GetProcessById() 的确切进程。
  • @NateBarbettini 希望你能给我提供一个代码。我对 vb.net 很陌生。
  • 问题中没有足够的信息来了解您的确切问题。为什么需要重新启动进程?你的程序应该如何知道要杀死哪一个?
  • @NateBarbettini 你在第一条评论中就有了想法,但不知道如何编码。

标签: vb.net process pid restart process.start


【解决方案1】:

不知道你在这里究竟想要什么......但是如果它在重新启动之前正在运行,这将杀死现有的游戏实例:

Private Games As New Dictionary(Of String, Process)

Private Sub GS1_Click(sender As Object, e As EventArgs) Handles GS1.Click
    Dim gameservercfg As String = GameServer1.Text
    Dim Key As String = gameservercfg.ToUpper

    If Games.ContainsKey(Key) Then
        If Not Games(Key).HasExited Then
            Games(Key).Kill()
        End If
        Games.Remove(Key)
    End If

    Dim psi As New ProcessStartInfo
    psi.FileName = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "GameServer.exe")
    psi.WorkingDirectory = System.IO.Path.GetDirectoryName(psi.FileName)
    psi.Arguments = gameservercfg
    Games.Add(Key, System.Diagnostics.Process.Start(psi))
End Sub

【讨论】:

  • 这段代码效果很好,但我希望这段代码放在一个单独的按钮中,仅用于重新启动。如果该特定 gs 未运行,则当单击按钮时,它将要求用户先运行它。 @Idle_Mind
猜你喜欢
  • 1970-01-01
  • 2014-11-06
  • 2016-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-08
  • 2012-04-29
  • 2012-12-11
相关资源
最近更新 更多