lstwsdm
lstwsdm

Reputation: 1

Time based process killing VB.NET

what I'm trying to do is to code an app for killing a process within a desired time(minutes). Main goal is check if the process is running , if it is running; ask to kill process, if not; still wait in the background and check if it is running. Im not a coder myself, just downloaded and tried to code a simple app. So excuse me for my bad coding skills.

Heres my code, I have 1 timer, 1 status bar and 1 button. Also heard that timers only handle 1 minute but I dont know how to convert it's interval to minutes.

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

        Timer1.Interval = 1800
        CheckIfRunning()

    End Sub
    Dim p() As Process
    Private Sub CheckIfRunning()
        p = Process.GetProcessesByName("Zoom") 'Process name
        If p.Count > 0 Then
            ' Process is running
            ToolStripStatusLabel1.Text = "ZOOM IS RUNNING"
        Else
            ' Process is not running
            ToolStripStatusLabel1.Text = "ZOOM IS NOT RUNNING"
        End If
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Dim ZOOM() As Process = Process.GetProcessesByName("Zoom")
        Dim ask As MsgBoxResult = MsgBox("30Min limit, Kill Zoom?", MsgBoxStyle.YesNo)
        If ask = MsgBoxResult.Yes Then

            For Each Process As Process In ZOOM
                Process.Kill()
                Timer1.Stop()
                Timer1.Interval = 1800
                Timer1.Start()
            Next
        Else
            Timer1.Stop()
            Timer1.Interval = 1800
            Timer1.Start()
        End If

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Timer1.Start()
    End Sub
End Class

It's sort of working but, when the 30 minute limit is complete, it spams the same messagebox. And I couldn't get it to work properly, so could use some help.

Upvotes: 0

Views: 300

Answers (1)

Enigmativity
Enigmativity

Reputation: 117064

The Interval value for a timer is the number of milliseconds, not seconds. So 1800 is 1.8 seconds, not 30 minutes.

Since in your Timer1_Tick you're almost immediately calling MsgBox and you say that you are getting message boxes spammed at you, I think you're probably using the wrong kind of timer. Make sure you're using the Windows Forms timer for a Windows Forms app.

It's also a good idea to call Timer1.Stop() before showing any UI to prevent spamming anyway.

Upvotes: 1

Related Questions