473,405 Members | 2,279 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

VB-APP: Check for running processes

Hi,

I'm trying to make my program give a MsgBox as soon as there's a certain process on such as 'notepad.exe'. I tried something in the lines of:

Expand|Select|Wrap|Line Numbers
  1.         Dim process = System.Diagnostics.Process.GetProcessesByName("notepad")
  2.  
  3.         If process = True Then
  4.             timer_checkforprocess.Enabled = False
  5.             MsgBox("blahblah", MsgBoxStyle.OkOnly)
  6.         End If
  7.  
I've also been searching around on the web and this site. I either dont use the right keywords or the answer isn't there.

How could I get this to work ?

Also, currently it's within a timer with an interval of 100. Does this increase CPU usage/memory usage ? If yes, is there a way to make it use as less cpu as possible ?

Thanks in advance,
Evolution445
Aug 28 '08 #1
15 4077
joedeene
583 512MB
Hi,

I'm trying to make my program give a MsgBox as soon as there's a certain process on such as 'notepad.exe'. I tried something in the lines of:

Expand|Select|Wrap|Line Numbers
  1.         Dim process = System.Diagnostics.Process.GetProcessesByName("notepad")
  2.  
  3.         If process = True Then
  4.             timer_checkforprocess.Enabled = False
  5.             MsgBox("blahblah", MsgBoxStyle.OkOnly)
  6.         End If
  7.  
I've also been searching around on the web and this site. I either dont use the right keywords or the answer isn't there.

How could I get this to work ?

Also, currently it's within a timer with an interval of 100. Does this increase CPU usage/memory usage ? If yes, is there a way to make it use as less cpu as possible ?

Thanks in advance,
Evolution445

i would set the timer at a less tick rate, such as 500 or 1000 or even less, depending on what your doing, and you could just use a simple for each statement such as...

Expand|Select|Wrap|Line Numbers
  1.  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2.         For Each myprocess In Process.GetProcesses
  3.             If myprocess.MainWindowTitle.Contains("Visual Basic") Then 'I only used this because i didnt have notepad running, notice the MainWindowTitle, there are other options such as just the ProcessName...
  4.                 MsgBox("Found It")
  5.  
  6.             End If
  7.             ListBox1.Items.Add(myprocess.ProcessName) 'Just adds all of the processes after done with the if statement, whether the if returns true or false...
  8.         Next
  9.     End Sub
this worked fine for me...

joedeene
Aug 28 '08 #2
Plater
7,872 Expert 4TB
I would think that your IF statement would always return false, since "process" would either be null(nothing) or an instance of the process object, and never a boolean.

And while I'm at it, if you're in VB.NET and not VB, you should be using the .NET constructs MessageBox.Show() and not MsgBox
Aug 28 '08 #3
And while I'm at it, if you're in VB.NET and not VB, you should be using the .NET constructs MessageBox.Show() and not MsgBox
I got both programs, VB2005 and VB.NET, I often confuse both and then my code gets mixed up. I meant VB.NET here.

Thanks joedeene, I now managed to work that out a little further by myself and its working :)

Thanks for the help.
Aug 28 '08 #4
Curtis Rutland
3,256 Expert 2GB
You missed Plater's point. VB.NET and VB are different languages. Visual Studio.NET uses VB.NET 2003, Visual Studio 2005 uses VB.NET 2005.

MsgBox is a construct from classic VB 6 and older. You should use the .NET constructs whenever and wherever you can.
Aug 28 '08 #5
joedeene
583 512MB
i apologize for the faulty code sample, i guess im already starting to pick up lazy habits of VB.net =(, but i was just showing a simple way to find the process name..

joedeene
Aug 28 '08 #6
Sorry to ask another thing related to this..

It now collects all processes inside a listbox, how would I make another messagebox display as soon as the same process appears in there twice or more ?

I thought something like this, but it just results in an error:

Expand|Select|Wrap|Line Numbers
  1.         If list_processes.Items.Count.CompareTo(process) = 2 Then
  2.         MessageBox.Show("You cannot have 2 instances of " & psname & " running at the same time!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly, False)
  3.         Me.Close()
  4.         End If
  5.  
Aug 31 '08 #7
joedeene
583 512MB
well some processes run two instances if they have different ones open ? like what if they are running two internet explorer browsers ? (process name "iexplore.exe")

joedeene
Aug 31 '08 #8
Yes, if my listbox contains 2 processes that have exactly the same name, I want to close my program, I know how to close it with "Me.Close()", but how to figure out if there's 2 of the same name.. That's where I'm a little stuck right now.
Aug 31 '08 #9
joedeene
583 512MB
well give a little more code sample fully of what you've got so far... i have to see how you are using your variables so i can know how to be of best help, i dont quite understand your code.

joedeene
Sep 1 '08 #10
I'm even having trouble understanding my own code. I've been busy for a few hours now on this little bit. I keep changing it, but it never seems to work, or .NET does different things than I want.

My first code is a few posts above here, this is my 2nd attempt:
Expand|Select|Wrap|Line Numbers
  1.         If list_processes.Items.Item(psname) = list_processes.Items.Count = 2 Then
  2.         MessageBox.Show("You cannot have 2 instances of " & psname & " running at the same time!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly, False)        Me.Close()
  3.         End If
  4.  
And just at the moment I'm thinking of something like this:

Expand|Select|Wrap|Line Numbers
  1.         Dim i, pscount As Integer
  2.         For i = 0 To list_processes.Items.Count - 1
  3.             pscount = list_processes.Items.Item(i).ToString.Contains(psname)
  4.         Next
  5.         If i = 2 Then
  6.             MessageBox.Show("You cannot have 2 instances of " & psname & " running at the same time!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly, False)
  7.             Me.Close()
  8.         End If
  9.  
The program I'm making is for a few games, and as soon as one of them has 2 windows open at the same time, I want my program to close. I thought the best way to find out if there's 2 instances running for that same game by checking the processlist, instead of using API FindWindow().


Evolution445
Sep 1 '08 #11
joedeene
583 512MB
I'm even having trouble understanding my own code. I've been busy for a few hours now on this little bit. I keep changing it, but it never seems to work, or .NET does different things than I want.

My first code is a few posts above here, this is my 2nd attempt:
Expand|Select|Wrap|Line Numbers
  1.         If list_processes.Items.Item(psname) = list_processes.Items.Count = 2 Then
  2.         MessageBox.Show("You cannot have 2 instances of " & psname & " running at the same time!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly, False)        Me.Close()
  3.         End If
  4.  
And just at the moment I'm thinking of something like this:

Expand|Select|Wrap|Line Numbers
  1.         Dim i, pscount As Integer
  2.         For i = 0 To list_processes.Items.Count - 1
  3.             pscount = list_processes.Items.Item(i).ToString.Contains(psname)
  4.         Next
  5.         If i = 2 Then
  6.             MessageBox.Show("You cannot have 2 instances of " & psname & " running at the same time!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly, False)
  7.             Me.Close()
  8.         End If
  9.  
The program I'm making is for a few games, and as soon as one of them has 2 windows open at the same time, I want my program to close. I thought the best way to find out if there's 2 instances running for that same game by checking the processlist, instead of using API FindWindow().


Evolution445
something as simple as this worked for me, but you'd have to ignore certain processes running two or more instances, such as system services/processes.


code.
Expand|Select|Wrap|Line Numbers
  1.   For Each proc In Process.GetProcesses
  2.             If ListBox1.Items.Contains(proc.ProcessName) Then
  3.                 MessageBox.Show("Process '" & proc.ProcessName & "' Running To Or More Instances")
  4.             End If
  5.             ListBox1.Items.Add(proc.ProcessName)
  6.  
  7.         Next
this code checks to see if the process name already exists before adding it to the listbox, now after messagebox.show(), place a me.close if necessary for you, but is this what you were looking for ?
Sep 1 '08 #12
It's rather like, if it appears in the listbox 2 times, a duplicate item. Then I want to close my program and give a messagebox. But I don't know if that's actually possible ?
I'm just stuck at the bit where to check if the same item appears twice or more times in the listbox.
Sep 1 '08 #13
joedeene
583 512MB
It's rather like, if it appears in the listbox 2 times, a duplicate item. Then I want to close my program and give a messagebox. But I don't know if that's actually possible ?
I'm just stuck at the bit where to check if the same item appears twice or more times in the listbox.

yes if the *ProcessName* appears in the listbox already, before adding the *ProcessName* it will check to see if the listbox contains the process name, you can also do many other options from the proc, like when you open notepad twice, you will notice 'notepad.exe' is listed there twice ? it will check for the Processes Name, not the main window title, such as 'Untitled - Notepad'.... ?

joedeene
Sep 1 '08 #14
I tried your code, but now the message pops up everytime as there is a duplicate going in the listbox, but it should only be giving the warning for a few certain processes. I now have something like this:

Expand|Select|Wrap|Line Numbers
  1.         For Each ps In Process.GetProcesses()
  2.             Dim processname As String
  3.             If list_processes.Items.Contains(ps.ProcessName.Contains(processname)) Then
  4.                 timer_checkforprocess.Enabled = False
  5.                 MessageBox.Show("Error!")
  6.                 Me.Close()
  7.             End If
  8.             list_processes.Items.Add(ps.ProcessName)
  9.  
  10.         Next
  11.  
But this doesnt result in anything
Sep 1 '08 #15
joedeene
583 512MB
I tried your code, but now the message pops up everytime as there is a duplicate going in the listbox, but it should only be giving the warning for a few certain processes. I now have something like this:

Expand|Select|Wrap|Line Numbers
  1.         For Each ps In Process.GetProcesses()
  2.             Dim processname As String
  3.             If list_processes.Items.Contains(ps.ProcessName.Contains(processname)) Then
  4.                 timer_checkforprocess.Enabled = False
  5.                 MessageBox.Show("Error!")
  6.                 Me.Close()
  7.             End If
  8.             list_processes.Items.Add(ps.ProcessName)
  9.  
  10.         Next
  11.  
But this doesnt result in anything

well ya ? when you said
Expand|Select|Wrap|Line Numbers
  1. Dim processname As String
you never set a value for the processname variable to search for, so its returning null, unless you left that part out of your code you posted?
Sep 1 '08 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: Terry | last post by:
Hi, I need some feedback. We are converting to .Net and we are trying to decide on whether to use VB.net or C#.net. As far as our current systems, they will probably be rewritten in ASP.Net. I...
13
by: Arsalan | last post by:
Is there any advantage in C# over VB.NET ? Or the difference is only syntax ? Can anything done in C# which cannot be done in VB.NET?
72
by: Robin Tucker | last post by:
I need to find some documents/research for my manager about VB.NET v C# use. I've noticed that there are many more people using C# than VB.NET, that there seem to be more job vacancies specifying...
28
by: Andy | last post by:
Any good resources regarding benefitis by using C3 over VB? /Andy
182
by: Jim Hubbard | last post by:
http://www.eweek.com/article2/0,1759,1774642,00.asp
19
by: lonelyplanet | last post by:
Hi, I'm studying for 70-306 using the book "MCAD/MCSD Visual Basic .NET Windows Applications" published by McGraw Hill (ISBN: 0-07-212583-7). I found the book has no programming exercise...
39
by: clintonG | last post by:
This is not about starting a fight but an observation that seems to be proving itself on its own merit and is therefore simply a point of conjecture. I did not get serious about writing software...
62
by: zacks | last post by:
A co-worker where I work is proposing all future code devopment be done in Visual C#. Here is his assessment of VB: VB.NET is hack as far as the CLR(Common Language Runtime) goes. It was...
14
by: John Smith | last post by:
Can someone convert from C# into VB this line for me: if (c is System.Web.UI.HtmlControls.HtmlForm)
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.