This IPH sample,
IPHTerminateOrphanedVSTA, uses a global mutext to start one, and only one, simple observer thread. The observer thread continues to execute while the vsta process is alive, whether or not a project is open.. The observer thread periodically checks for existence of the hostProcess. If there is no hostProcess running, and there is an orphaned vsta.exe, the observer thread shuts down the orphan vsta process before exiting. The IPH allows opening the VSTA as a standlone IDE (observer thread is not started).
You should even be able to lift the code from IPH.cs and use it almost directly in your vsta projects' existing IPH:
hostProcessName =
"ShapeAppCSharp";
is the only thing you'll need to change in IPH.cs to work with your host process.
To run the attached sample, follow these 5 steps. The steps assume 1. ShapeAppMacroRecordingCSharp is the host; 2. that you have already installed, run ShapeAppMacroRecordingCSharp (from VSTA 2.0 SDK Release bits) and launched the IDE at least once; And 3. that you will move out or rename the original ShapeAppMacroRecordingCSharp solution folder tree and drop this solution in its place (preserving registered name and folderpath):
1. Build IPHTerminateOrphanedVSTA project along with the ShapeAppMacroRecordingCSharp solution. This should create the assembly:
ShapeAppMacroRecordingCSharp\IPHTerminateOrphanedVSTA\IPHTerminateOrphanedVSTA\bin\Debug\IPHTerminateOrphanedVSTA.dll
and should also install it into the GAC (via InstallInGAC.bat), listing the asembly info in the VS output window:
"The Global Assembly Cache contains the following assemblies:
IPHTerminateOrphanedVSTA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5993c97a24fb6aa3, processorArchitecture=MSIL"
2. Run the IPHTerminateOrphanedVSTA.IHP.reg file (double-click on it from Windows Explorer) included in the IPHTerminateOrphanedVSTA project to add IPH to registry.
3. Explore the ShapeAppMacroRecordingCSharp\templates\CSharp\ShapeAppCSharp-AppLevel.zip, and confirm that ShapeAppCSharpAppAddInProject.csproj includes the IPH entry:
<InProcHost>{2CD68381-5859-4afe-88AC-42D13CD4B335}</InProcHost>
4. Update the HKEY_LOCAL_MACHINE\Software\Microsoft\VSTAHost\ShapeAppCSharp registry hive so host app will use newly registered IPH (from step 2):
Delete the 9.0 key under the ShapeAppCSharp key, HKEY_LOCAL_MACHINE\Software\Microsoft\VSTAHost\ShapeAppCSharp\9.0,
then, run vsta /hostid ShapeAppCSharp /setupfrom the command line
5. Finally, follow the instructions in IPH.cs to run ShapeAppCSharp and test the IPH:
//To debug this IPH follow this sequence:
//1. start ShapeAppMacroRecordingCSharp sample directly from .exe or select 'Debug | Start Without Debugging' in VS
//2. Show VSTA IDE (Alt + F11), should load a macro project with no IPH
// (if macro project has IPH, just remove entry <InProcHost>{2CD68381-5859-4afe-88AC-42D13CD4B335}</InProcHost> from ShapeAppMacros.csproj before 'Tools | ShowIDE' opens it).
//3. In VS, 'Debug | Attach To Process...' select vsta.exe
//4. In VSTA IDE, 'File | New Project', select CSharp template, discard open project and this IPH (IPHTerminateOrphanedVSTA.IPH.SetAdapter() should be called.
//5. From 'Task Manager', kill ShapeAppCSharp.exe and the CheckForHostObserverThread.CheckforHostProcess should drop into section where this call is made:
// System.Diagnostics.Process vstaProc = System.Diagnostics.Process.GetCurrentProcess(); so that vsta.exe can be terminated.