Question How do I start a process UNLESS it's already started

robertb_NZ

Well-known member
Joined
May 11, 2010
Messages
146
Location
Auckland, New Zealand
Programming Experience
10+
When Jazz has generated COBOL and other objects, it can execute
Dim FileNPath As String = "C:\tutorials\MSJazz1\MSJazz1.sln"
Dim MFProject As Diagnostics.Process = System.Diagnostics.Process.Start(FileNPath)​
to invoke Visual Studio to open a COBOL project to test the program.

But if the VS project is already open, this will create a second instance.

How do I detect that the .sln is already open so that I can do something else?
 
The solution file isn't a process, so there's only so much you can do. One option would be to call Process.GetProcessesByName to get a list of VS processes. If there are any, check the MainWindowTitle of each to see if the solution name is included.
 
I'd hoped that there was a simpler solution
None that I can think of. You could possibly check to see whether the SLN file was open but there's no simple way to do that and it wouldn't necessarily be open in VS anyway.
 
I'm struggling with Process.GetProcessesByName. Of the ~171 processes on my laptop none have a name that's obviously Visual Studio. I'm wondering about using GetProcesses() and then looping through looking for
IF Process.Startinfo.Filename = "C:\tutorials\MSJazz1\MSJazz1.sln" Then ...​
but I can't get that to work at the moment. I think it's time to knock off for the evening and pour myself a glass of wine!
 
Module Module1

    Sub Main()
        For Each p In Process.GetProcesses()
            Console.WriteLine("{0}: {1}", p.ProcessName, p.MainWindowTitle)
        Next

        Console.ReadLine()
    End Sub

End Module

Part of the output from that was the following:
devenv: ConsoleApp1 (Running) - Microsoft Visual Studio
That just happens to be the process and main window that was running the code above. "devenv.exe" has been the name of the VS executable since the very first .NET version and possibly earlier. "ConsoleApp1" was the name of the solution in this case.
 
Thanks, that was very helpful. I wrote
VB.NET:
For Each P In System.Diagnostics.Process.GetProcessesByName("devenv")
   Debug.Print("Process. Name:" & P.ProcessName & ", MainWindowTitle:" & P.MainWindowTitle & ", Filename:" & P.StartInfo.FileName)
Next

After Jazz has started the MSJazz1 solution, Debug.print shows
Process. Name:devenv, MainWindowTitle:Jazz (Debugging) - Microsoft Visual Studio , Filename:
Process. Name:devenv, MainWindowTitle:MSJazz1 - Microsoft Visual Studio , Filename:​

So if I use GetProcessesByName("devenv") and test for the project name ("MSJazz1" in this case) at the start of MainWindowTitle, I should be OK. Do you know if "devenv" will also work for Eclipse as well as Visual Studio?

Thank you, Robert.
 
Do you know if "devenv" will also work for Eclipse as well as Visual Studio?

I very much doubt it. You have to determine what the EXE name was for Eclipse.
 
Back
Top