Results 1 to 13 of 13

Thread: How could i kill a application

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    36

    How could i kill a application

    How would i kill an application, I'm creating my first project in VB and this is my code so far:

    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Shell("samp-server.exe")
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    
        End Sub
    End Class
    How could i kill samp-server.exe in button2?, Also how could i echo a .txt file into a TextBox?

    Thanks,

    Chris

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How could i kill a application

    You shouldn't be using Shell for a start. In VB.NET you should be calling Process.Start to start a new process. You will then have a Process object to refer to. You can then call its CloseMainWindow method if it has a main window and its Kill method if it doesn't.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    36

    Re: How could i kill a application

    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Process.Start("samp-server.exe")
        End Sub
    I have completed that like you stated, But i am unsure on how i would kill this?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How could i kill a application

    The Process.Start method returns a Process object. You can assign that object to a variable and then access it later, e.g. in the Button2_Click method.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    36

    Re: How could i kill a application

    Quote Originally Posted by jmcilhinney View Post
    The Process.Start method returns a Process object. You can assign that object to a variable and then access it later, e.g. in the Button2_Click method.
    This right?:

    Code:
    Public Class Form1
        Dim samp;
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            samp = Process.Start("samp-server.exe")
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Kill("samp-server.exe")
    
        End Sub
    End Class

  6. #6
    Frenzied Member Pc_Not_Mac's Avatar
    Join Date
    Oct 2009
    Location
    localhost
    Posts
    1,206

    Re: How could i kill a application

    This is a very good example you should check it out.

    http://www.codeproject.com/KB/vb/killVBprocess.aspx
    My Codebank:
    Windows Vista & 7 Glass Effect & Limit the amount of times your application could be opened.
    Pause Your Code & Check the OS name

    The question of whether computers can think is like the question of whether submarines can swim.

    Currently learning: Java

    Coding can be a learning experience or

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How could i kill a application

    Quote Originally Posted by ChrisMartino View Post
    This right?:

    Code:
    Public Class Form1
        Dim samp;
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            samp = Process.Start("samp-server.exe")
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Kill("samp-server.exe")
    
        End Sub
    End Class
    If the 'samp' variable is type Porcess then you should be declaring it as type Process:
    vb.net Code:
    1. Dim samp As Process
    Then, as I said, you are supposed to refer to that Process in the Button2_Click method. If you don't then it serves no purpose:
    vb.net Code:
    1. samp.Kill()
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    36

    Re: How could i kill a application

    Works BRILLIANT GUYS, So happy its my first program , How could i do it to check if the process is running and if it isn't it starts it up?

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How could i kill a application

    The link that Pc_Not_Mac provided shows an example that calls Process.GetProcesses and then checks the ProcessName of each one. You could do that, or you could call Process.GetProcessesByName and specify the name of the process. If that returns an empty array then the process is not running.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    36

    Re: How could i kill a application

    Quote Originally Posted by jmcilhinney View Post
    The link that Pc_Not_Mac provided shows an example that calls Process.GetProcesses and then checks the ProcessName of each one. You could do that, or you could call Process.GetProcessesByName and specify the name of the process. If that returns an empty array then the process is not running.
    I was thinking a timer or something? sorry im not sure on how to do a array

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How could i kill a application

    You don't have to "do" an array. Calling GetProcessesByName will return an array. Have you checked out the link provided earlier. Also, whether you use a Timer or whatever, you're still going to have to call that method to find out whether the process is running.

    If you're not sure about using arrays then maybe you should work your way through a beginner tutorial, which cover that and other basic concepts. You could also just look at a tutorial on arrays specifically.

    http://www.homeandlearn.co.uk/NET/vbNET.html
    http://www.startvbdotnet.com/language/arrays.aspx
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    36

    Re: How could i kill a application

    Ok, I found this on the internet, How would i make it to check if 'samp' is running?

    Code:
    If
    UBound(Diagnostics.Process.GetProcessesByName(Diag nostics.Process.GetCurrent
    Process.ProcessName)) > 0 Then
    
    MessageBox.Show("Process running", "Message", MessageBoxButtons.OK,
    MessageBoxIcon.Warning)
    
    else
    
    MessageBox.Show("Process not running", "Message", MessageBoxButtons.OK,
    MessageBoxIcon.Warning)
    
    End
    
    End If

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How could i kill a application

    First up, that code is not going to work. You shouldn't use UBound at all in VB.NET anyway but that code is going to tell you that the process isn't running if UBound returns 0, which is wrong.

    As I said before, you call GetProcessesByName and specify the name of the process. What's the name of the process you're looking for? That's the name you specify when you call GetProcessesByName. That will return an array of Process objects; one for each process it finds with that name. If that array is empty then there are no processes with that name. To check whether an array is empty you test its Length property.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width