简体   繁体   中英

Opened program with EnvDTE, closes immediately

I'm trying to launch Visual Studio 2017 and open a solution programmatically, as shown in the code below. Problem is, when I run it VS2017 opens, loads the solution and closes immediately. I have searched and not found anyone with similar issues with similar code as the solution to opening a VS solution. What might cause this behaviour and how do I prevent VS2017 from closing immediately? An exception is never encountered.

private void OpenVisualStudio(string file)
    {
        try
        {
            System.Type t = Type.GetTypeFromProgID("VisualStudio.DTE.15.0");
            EnvDTE80.DTE2 dte = (EnvDTE80.DTE2)System.Activator.CreateInstance(t);
            dte.MainWindow.Visible = true;
            dte.Solution.Open(file);
        }
        catch (Exception e)
        {
            Logger.GetInstance().Write(e.StackTrace, e.Message, LoggingLevel.Error);
        }
    }

Update: If I place a breakpoint at the end of the scope and hit continue after VS is done loading, it does not close. After checking Task Manager it seems the instances of Visual Studio are not really closed at all...only the window is. Even after manually exiting the programmatically created VS instance and stopping the debugging session the programmatically created instance remains visible in Task Manager.

There is dte.MainWindow.Activate() . Have you tried using that? There is a known problem with VS instances remaining in the task manager and not closing completely but it happens only when VS is forced/abruptly closed.

Set the DTE.UserControl property to true and VS will no longer close.

Relevant paragraph from the documentation:

If the environment is not under user control, and the last external automation client disconnects, then the environment shuts down.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM