none
i need help with my 13 lines of code (run time error) RRS feed

  • Question

  • hello , i managed to make a simple app that kills notepad.exe when Esc Key Is pressed no matter in which window i am

    here's the code :

    Public Class Form1
        Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Integer) As Short
        Private Const ESCAPE = &H1B
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Dim P As Process() = Process.GetProcessesByName("notepad")
            If (GetAsyncKeyState(ESCAPE)) Then
                P(0).Kill()
                MessageBox.Show("notepad has been terminated successfully", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information)
                End
            End If
        End Sub
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Timer1.Start()
        End Sub
    
    End Class

    now when i run the code i get this error , even when i compile it :

    https://i.imgur.com/YqP9JPK.png

    im new for all this and have no idea what's happening 

    any help would be appreciated <3 

    ps: im using vb 2008 express 


    • Edited by Tarwada Tuesday, July 31, 2018 11:50 AM
    Tuesday, July 31, 2018 11:49 AM

All replies


  • here's the code :

    Public Class Form1
        Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Integer) As Short
        Private Const ESCAPE = &H1B
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Dim P As Process() = Process.GetProcessesByName("notepad")
            If (GetAsyncKeyState(ESCAPE)) Then
                P(0).Kill()
    
    
    

    now when i run the code i get this error , even when i compile it :

    https://i.imgur.com/YqP9JPK.png



    The error occurs because there is nothing in the P array. This will happen
    if Notepad isn't running. You should test the Length of P before trying
    to access any elements in it. Example:

    If P.Length = 0 Then Console.WriteLine("Notepad not running!")

    - Wayne

    Tuesday, July 31, 2018 1:02 PM
  • Apparently notepad is not open when that happens? Perhaps it had already been closed?

    You can check the p.count value first and then turn off the timer too ie:

    Dim P As Process() = Process.GetProcessesByName("notepad") If P.Count > 0 Then If (GetAsyncKeyState(ESCAPE)) Then P(0).Kill()

    timer1.stop MessageBox.Show("notepad has been terminated successfully", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information) End End If End If


    • Proposed as answer by Alex Li-MSFT Wednesday, August 1, 2018 1:51 AM
    Tuesday, July 31, 2018 1:03 PM
  • If you started note pad in your app then you can know when it exits by say pressing the red X.

    Conceptual example

    Public Class Form1
        Private _active As Boolean = False
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim p As New Process()
            AddHandler p.Exited, AddressOf p_Exited
            p.StartInfo.FileName = "C:\OED\doc.txt"
            p.EnableRaisingEvents = True
            p.Start()
            _active = True
        End Sub
    
        Private Sub p_Exited(sender As Object, e As EventArgs)
            MessageBox.Show("Closed")
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            If _active Then
                MessageBox.Show("Close it")
                _active = False
            Else
                MessageBox.Show("Already closed or has not been opened")
            End If
        End Sub
    End Class
    

    If you were using a newer release of VS.

    Public Class Form1
        Private _active As Boolean = False
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim p As New Process()
            AddHandler p.Exited, Sub(sender1, ee) ' do whatever
                                     MessageBox.Show("Closed")
                                 End Sub
            p.StartInfo.FileName = "C:\OED\doc.txt"
            p.EnableRaisingEvents = True
            p.Start()
    
            _active = True
        End Sub
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            If _active Then
                MessageBox.Show("Close it")
                _active = False
            Else
                MessageBox.Show("Already closed or has not been opened")
            End If
    
        End Sub
    End Class
    
    Granted this may not be exactly what you are looking for e.g. kill an instance of note pad started outside your project, not sure if that is what you want or not or too kill a process for note pad you fired up.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, July 31, 2018 1:57 PM
    Moderator
  • Hi

    I agree with the code written by tommytwotrain. When you open one or more notepads, you need to close Timer1 in time after kill notepad.

    Of course you can add one more judgment.

     If (GetAsyncKeyState(ESCAPE)) And P.Count > 0 Then
                P(0).Kill()
                MessageBox.Show("notepad has been terminated successfully", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information)
    
            End If

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, August 1, 2018 2:05 AM