How to make Application close if program found.

Posts 1–11 of 11 · Page 1 of 1
How to make Application close if program found.
Hey im wondering how can i make vb 2010 application close if program is found i mean like i want to do like this: Waiting for notepad.. .. Notepad found and then program closes!
check whether there is a process "notepad" running
that is what i have to search from google right ? :P
Quote Originally Posted by zarix View Post
that is what i have to search from google right ? :P
Just add a small function like this
Code:
    Sub CloseIfFound(ByVal ProcessName As String)
        Dim P = Process.GetProcessesByName(ProcessName)
        If P.Length = 0 Then Return
        Application.Exit()
    End Sub
And on a timer
Code:
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        CloseIfFound("notepad")
    End Sub
Or add it directly into the timer
Code:
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim P = Process.GetProcessesByName("notepad")
        If P.Length = 0 Then Return
        Application.Exit()
    End Sub
Dim P = Process.GetProcessesByName("notepad")
Will look for notepad in the process list and store it in an array of processes.


If P.Length = 0 Then Return
If none matching notepad are found, the array will be empty, hence returning if the value is zero.

Application.Exit()
If one or more matching the process name you're after is found, the code will never return and Application.Exit() gets called closing your form.
Quote Originally Posted by Pingo View Post
Just add a small function like this
Code:
    Sub CloseIfFound(ByVal ProcessName As String)
        Dim P = Process.GetProcessesByName(ProcessName)
        If P.Length = 0 Then Return
        Application.Exit()
    End Sub

And on a timer
Code:
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        CloseIfFound("notepad")
    End Sub
Or add it directly into the timer
Code:
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim P = Process.GetProcessesByName("notepad")
        If P.Length = 0 Then Return
        Application.Exit()
    End Sub
Dim P = Process.GetProcessesByName("notepad")
Will look for notepad in the process list and store it in an array of processes.


If P.Length = 0 Then Return
If none matching notepad are found, the array will be empty, hence returning if the value is zero.

Application.Exit()
If one or more matching the process name you're after is found, the code will never return and Application.Exit() gets called closing your form.
same function u use in in injector except u use (textbox1 + ".exe")
If you use Linq you can check if two lists intersect easily to find blacklisted applications you want to close. Like this:

Code:
		Private Shared ReadOnly m_BlacklistApplications As New List(Of String)() From { _
			"notepad", _
			"calc" _
		}

		If procs.Intersect(m_BlacklistApplications).Any() Then
			Application.Exit()
		End If
Quote Originally Posted by atom0s View Post
If you use Linq you can check if two lists intersect easily to find blacklisted applications you want to close. Like this:

Code:
		Private Shared ReadOnly m_BlacklistApplications As New List(Of String)() From { _
			"notepad", _
			"calc" _
		}

		If procs.Intersect(m_BlacklistApplications).Any() Then
			Application.Exit()
		End If
Or just use the default built in "contains" function...
Code:
if procs.contains(m_blacklistapplications) then
/close
end if
Quote Originally Posted by rileyjstrickland View Post
Or just use the default built in "contains" function...
Code:
if procs.contains(m_blacklistapplications) then
/close
end if
Contains only compares against a single entry, not a collection.
Quote Originally Posted by atom0s View Post
Contains only compares against a single entry, not a collection.
Just do a loop then.
Code:
for each item in blacklist
if procs.contains(item)
close = true
exit for
end if
next
if close then
me.close
end if
Quote Originally Posted by rileyjstrickland View Post
Just do a loop then.
Code:
for each item in blacklist
if procs.contains(item)
close = true
exit for
end if
next
if close then
me.close
end if
And that was the whole point of my post, to avoid a loop altogether..
Call this method on a separate thread:

Code:
Private Sub Check()
        Dim active As Boolean = False
        Dim proc() As Process
        Do Until active = True
            proc = Process.GetProcessesByName("notepad")
            If proc.Count > 0 Then active = True
        Loop
        End
    End Sub
The only thing I don't like about it is that 'End' crosses threads illegally, although it is possible to get away with, it's bad practice. If you want to truly resolve it, look into delegates.
Posts 1–11 of 11 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?