How to reliably add projects to solution programmatically

0

The key word here is "reliably".

When I create solution and add few projects I will get an exception:

Unhandled Exception: System.Runtime.InteropServices.COMException: The message 
  filter indicated that the application is busy. (Exception from HRESULT: 
  0x8001010A (RPC_E_SERVERCALL_RETRYLATER))
  at EnvDTE.Project.get_Object() 

As ugly hack I added Thread.Sleep to avoid timeouts and this works, but of course it is not solution per se. So how can I add projects to solution in a reliable way -- i.e. without explicit sleeps and on other hand without timeouts?

I use VS 2017 and I create VS instance and solution like this:

System.Type type = System.Type.GetTypeFromProgID("VisualStudio.DTE.15.0",
    throwOnError: true);
Object obj = System.Activator.CreateInstance(type, nonPublic: true);
var dte = (EnvDTE80.DTE2)obj;
dte.MainWindow.Visible = true; // optional if you want to See VS doing its thing

// create a new solution
dte.Solution.Create(sln_path, sln_name);
var solution = (EnvDTE80.Solution2)dte.Solution;

Having the solution at hand I loop like this:

  • I call solution.AddFromTemplate so I get VSLangProj.VSProject
  • when I need it I add references to current project curr_prj.References.AddProject(other_prj)

Without sleep I get timeout right before first adding a reference, so at minimum I am able to add first project, second project and then I have timeout on adding reference to the first project.

visual-studio
timeout
envdte
asked on Stack Overflow Nov 5, 2018 by astrowalker • edited Nov 5, 2018 by astrowalker

1 Answer

1

Try implementing IOleMessageFilter error handlers.

See:

How to: Fix 'Application is Busy' and 'Call was Rejected By Callee' Errors https://msdn.microsoft.com/en-us/library/ms228772.aspx

answered on Stack Overflow Nov 5, 2018 by Carlos Quintero

User contributions licensed under CC BY-SA 3.0