ShapeAppMacroRecordingCSharp With IPHTerminateOrphanedVSTA observer thread


posted by Gary
09-09-2008

Downloads: 114
File size: 247.9kB
Views: 398
ShapeAppMacroRecordingCSharp With IPHTerminateOrphanedVSTA observer thread

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.

Comments

Melody wrote re: ShapeAppMacroRecordingCSharp With IPHTerminateOrphanedVSTA observer thread
on 09-25-2008 5:07 PM
Be sure that you have previously run the SetupShapeAppMacroRecordingCSharp.js setup file included wit the sample. If this file hasn't been run an error message "Unable to load context" will appear when you attempt to launch the IDE.
Gary wrote re: ShapeAppMacroRecordingCSharp With IPHTerminateOrphanedVSTA observer thread
on 09-26-2008 11:28 AM
Alexandre Aupy describes his improvements to the design: . . . . * On the host app side, when I start VSTA, I create a global mutex named after the host app PID (retrieved using Process interface) and I store it as member data. . . . . * When VSTA is stopped (host app exits), I release it properly. . . . . * On the IPH side, in the SetAdapter method, I use a ManagementObject to retrieve the parent PID (i.e. the PID of the host app), . . . . * Then I create a different Global mutex named after this unique PPID. This ensures that only one monitoring thread exists per host app/vsta.exe couple. . . . . * If I am the first instance creating this mutex, I start my monitoring thread. . . . . * This monitoring thread opens the first global mutex, named after the PPID, and waits for it. . . . . * If it cannot be opened (exception), it is a fatal error. . . . . * If I get an Abandoned mutex exception, it means that the host app has crashed without releasing properly the global mutex. Hence I quit the DTE. . . . . * Otherwise, my mutex was released properly at the end of life of the host app, I release it too properly and exit my thread. . . . .
Gary wrote re: ShapeAppMacroRecordingCSharp With IPHTerminateOrphanedVSTA observer thread
on 09-26-2008 11:33 AM
Probably better to simply call vstaProc.kill and not call vstaProc.CloseMainWindow, because it does not guarantee termination.
Ben_Wang wrote re: ShapeAppMacroRecordingCSharp With IPHTerminateOrphanedVSTA observer thread
on 09-27-2008 5:14 AM
As there can be several processes with the same name, how can I determine which is the real host process?
Copyright Summit Software Company, 2008. All rights reserved.