Share via

Need to stop CMD line process!

Simon Scott 306 Reputation points
2021-04-23T08:48:26.497+00:00

Morning all,

I run the following code which works well. However it keeps creating new CMD line windows which isn't ideal as it consumes memory!

I have tried proc.kill but the process doesn't start again!

What can i do to stop the process?

Dim sSubString = sString.Substring(sString.LastIndexOf("|") + 1).Trim()
Dim procStartInfo As System.Diagnostics.ProcessStartInfo = New System.Diagnostics.ProcessStartInfo("cmd", "/k" & ("tzutil.exe /s " + """" + sSubString + """"))
'Dim procStartInfo As System.Diagnostics.ProcessStartInfo = New System.Diagnostics.ProcessStartInfo("cmd", "/k" & ("tzutil.exe /s '" + sSubString + "'"))
Dim proc As System.Diagnostics.Process = New System.Diagnostics.Process()
proc.StartInfo = procStartInfo
procStartInfo.WindowStyle = ProcessWindowStyle.Hidden
proc.Start()

Many thanks
Simon

Developer technologies | VB

Answer accepted by question author

Viorel 127K Reputation points
2021-04-23T13:24:29.507+00:00

Try "/c" instead of "/k".

Maybe consider starting the program directly, without "cmd".

Was this answer helpful?

0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Simon Scott 306 Reputation points
    2021-04-23T13:51:45.437+00:00

    That's it!!!

    Changed /k to /c and it works as it should.

    Thank you so much.

    Simon

    Was this answer helpful?

    0 comments No comments

  2. Simon Scott 306 Reputation points
    2021-04-23T13:14:01.49+00:00

    Apologies, here is the code....

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick

        'Test for IP address subnet
        Dim sString As String = Nothing
        Dim strIPAddress As String
        Dim strhostname As String
        Dim Prefix As String
        Try
            strhostname = System.Net.Dns.GetHostName()
            strIPAddress = System.Net.Dns.GetHostByName(strhostname).AddressList(0).ToString()
            Prefix = strhostname.Substring(0, 3)
    
            'Check the IP address of the local machine
            Dim ipArray() As String = Split(strIPAddress, ".")
            '  If ipArray(0) <> "10" Then
            '     LblShip.ForeColor = Color.Red
            '     LblStatus.ForeColor = Color.Red
            ' LblShip.Text = "Unable to identify location"
            '     LblStatus.Text = "Timezone not updating, check IP address!"
            '     Exit Sub
            ' End If
    
            'Test
            If ipArray(1) = "168" And ipArray(2) = "150" Then
                LblShip.Text = "This PC is at Head Office"
                Using sr As New StreamReader("c:\temp\timezone.ini")
                    sString = sr.ReadToEnd()
                End Using
    
                                    Else
                                        LblShip.ForeColor = Color.Red
                                        LblStatus.ForeColor = Color.Red
                                        LblShip.Text = "Unable to identify location"
                                        LblStatus.Text = "Timezone not updating, check IP address!"
                                        Exit Sub
                                    End If
    
        Catch ex As Exception
            MsgBox(ex.Message)
            Exit Sub
    
        End Try
    
        Dim sSubString = sString.Substring(sString.LastIndexOf("|") + 1).Trim()
        Dim procStartInfo As System.Diagnostics.ProcessStartInfo = New System.Diagnostics.ProcessStartInfo("cmd", "/k" & ("tzutil.exe /s " + """" + sSubString + """"))
         Dim proc As System.Diagnostics.Process = New System.Diagnostics.Process()
        proc.StartInfo = procStartInfo
        procStartInfo.WindowStyle = ProcessWindowStyle.Hidden
        proc.Start()
        proc.WaitForExit()
    

    The timer start event is in the form load event

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Timer1.Interval = 10000
        Timer1.Start()
    

    Thanks
    Simon

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.