Results 1 to 8 of 8

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

  1. #1
    .NET Framework
    .NET 4.0
    Join Date
    May 2010
    Location
    Auckland, New Zealand
    Posts
    108
    Reputation
    106

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

    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?

  2. #2
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,995
    Reputation
    1727
    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.

  3. #3
    .NET Framework
    .NET 4.0
    Join Date
    May 2010
    Location
    Auckland, New Zealand
    Posts
    108
    Reputation
    106
    Thanks, I'd seen GetProcessesByName in the page about the PROCESS object, but I'd hoped that there was a simpler solution

  4. #4
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,995
    Reputation
    1727
    Quote Originally Posted by robertb_NZ View Post
    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.

  5. #5
    .NET Framework
    .NET 4.0
    Join Date
    May 2010
    Location
    Auckland, New Zealand
    Posts
    108
    Reputation
    106
    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!

  6. #6
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,995
    Reputation
    1727

    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.

  7. #7
    .NET Framework
    .NET 4.0
    Join Date
    May 2010
    Location
    Auckland, New Zealand
    Posts
    108
    Reputation
    106
    Thanks, that was very helpful. I wrote
    Code:
    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.

  8. #8
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,995
    Reputation
    1727
    Quote Originally Posted by robertb_NZ View Post
    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.

Similar Threads

  1. How would I reference a process that is already started?
    By GingerCode in forum VB.NET General Discussion
    Replies: 6
    Last Post: 08-15-2014, 5:14 AM
  2. Replies: 0
    Last Post: 02-03-2009, 11:20 AM
  3. Question Process started/terminated event?
    By DragonQ in forum VB.NET General Discussion
    Replies: 8
    Last Post: 11-06-2008, 1:34 PM
  4. process.start
    By .paul. in forum VB.NET General Discussion
    Replies: 10
    Last Post: 11-21-2007, 11:25 PM
  5. How to check whether a System.Diagnostics.Process is started?
    By cjard in forum VB.NET General Discussion
    Replies: 2
    Last Post: 12-19-2006, 1:42 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •