1

I am working with VB.NET 2013 Express.

I have a solution containing two projects, let's say AA and BB.

AA is a regular WinForms app.

BB is a windowless WinForms app.

AA may be opened and closed many times throughout the session. When AA opens, it should see if BB is already running, and if not, launch it.

BB will run until the PC (Windows 7) is powered down.

I need some kind of way of testing if BB is already running.

I tried writing status to a file, but I do not know how to get a closing event on BB to reset status for the next session.

I really do not want to add anything to computer's start-up menu if I can avoid it. Is there a better way?

mcu
  • 3,302
  • 8
  • 38
  • 64
  • [Walkthrough: Creating a Windows Service Application](https://msdn.microsoft.com/en-us/library/zt39148a(v=vs.110).aspx) – Remus Rusanu Jul 12 '15 at 21:07

2 Answers2

1
bool bbexists = System.Diagnostics.Process.GetProcessesByName("BB").Length > 0 ;

If needed, you can get/check BB process name:

System.Diagnostics.Process.Process.GetCurrentProcess.ProcessName 
Graffito
  • 1,658
  • 1
  • 11
  • 10
1

It depends on exactly what you need, but two ways come to mind.

Check to see if the BB process is running

This is the easiest and quickest way, in my opinion. Just check to see if the BB process is running before hand, then do whatever:

bool IsProcessRunning(string processName)
{
        return (System.Diagnostics.Process.GetProcessesByName(processName).Length != 0);
}

Make BB a single instance form and just call it every time from AA

See this post about making single instance WinForms

What is the correct way to create a single instance application?

This way you can just call BB every time without needing to first check if it's running.

Equalsk
  • 7,954
  • 2
  • 41
  • 67
  • For some reason, this is not working. I always get an empty array of processes. The task manager appends `" *32"` to the executable name, I have tried doing the same, but that did not help, I still got a zero length array. – mcu Jul 12 '15 at 21:58
  • Have you tried running the app as admin? Sounds like you don't have permission to check running processes. Maybe you can add some code to your answer if it's still not working because my way is definitely right ;-) – Equalsk Jul 12 '15 at 22:13
  • I got it working using Mutex. Thanks for the tip. It seems to be a cleaner solution too, though I may be wrong. I basically followed this example here. http://stackoverflow.com/a/2627466/2228746 – mcu Jul 12 '15 at 22:21