programmatically checking if app is running

M

Mike P

I am programmatically starting and ending a local instance of Outlook :

//programmatically start local Outlook
System.Diagnostics.Process proc = new
System.Diagnostics.Process();
proc.StartInfo.FileName = "outlook.exe";
proc.Start();
///code
proc.Kill();

Does anybody know how I can check if it is already running before
deciding whether or not to start it?
 
D

DBC User

I am programmatically starting and ending a local instance of Outlook :

//programmatically start local Outlook
System.Diagnostics.Process proc = new
System.Diagnostics.Process();
proc.StartInfo.FileName = "outlook.exe";
proc.Start();
///code
proc.Kill();

Does anybody know how I can check if it is already running before
deciding whether or not to start it?

*** Sent via Developersdexhttp://www.developersdex.com***


I had the same question and following answer solved my problem;

If you want to get a list of Processes that have a window try this
line of code.

ArrayList programlist = new ArrayList();
Process[] procList = Process.GetProcesses(userName);

for (int i = 0; i < procList.Length; i++)
{
if (procList.MainWindowHandle != IntPtr.Zero)
{
programlist.Add(procList.ProcessName + "\t" +
procList.MainWindowTitle);
}


This assumes you know the name of the logged in user (It will only get
the applications launched by the specified user, but that should be
fine for your purposes). If you don't you can obtain it through the
System.Environment class I believe.

When this code is done you will have the list of applications shown in
the Windows Task Manager. You can play around with the different
attributes like ProcessName and MainWindowTitle to find the
information you're looking for. Hope this helps. If you have any
problems with it just post back here. Good luck.

~ Justin Creasy


Thanks.
 
N

Nicholas Paldino [.NET/C# MVP]

In this case, that might not be the best idea. The assumption is that
the user is actually going to DO something with Outlook. In this case, the
user is better off calling the static GetActiveObject method on the Marshal
class to get the object registered in the Running Object Table. Assuming it
returns an object, it will be the currently running instance of Outlook,
which the OP can access through Automation to make it do what he/she wants.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

DBC User said:
I am programmatically starting and ending a local instance of Outlook :

//programmatically start local Outlook
System.Diagnostics.Process proc = new
System.Diagnostics.Process();
proc.StartInfo.FileName = "outlook.exe";
proc.Start();
///code
proc.Kill();

Does anybody know how I can check if it is already running before
deciding whether or not to start it?

*** Sent via Developersdexhttp://www.developersdex.com***


I had the same question and following answer solved my problem;

If you want to get a list of Processes that have a window try this
line of code.

ArrayList programlist = new ArrayList();
Process[] procList = Process.GetProcesses(userName);

for (int i = 0; i < procList.Length; i++)
{
if (procList.MainWindowHandle != IntPtr.Zero)
{
programlist.Add(procList.ProcessName + "\t" +
procList.MainWindowTitle);
}


This assumes you know the name of the logged in user (It will only get
the applications launched by the specified user, but that should be
fine for your purposes). If you don't you can obtain it through the
System.Environment class I believe.

When this code is done you will have the list of applications shown in
the Windows Task Manager. You can play around with the different
attributes like ProcessName and MainWindowTitle to find the
information you're looking for. Hope this helps. If you have any
problems with it just post back here. Good luck.

~ Justin Creasy


Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top