Skip to content

Commit

Permalink
前回起動したPowerShellプロセスを記憶して、起動時に強制終了するようにした。
Browse files Browse the repository at this point in the history
closed #3
  • Loading branch information
tanaka-takayoshi committed Apr 4, 2015
1 parent 15cee40 commit 1939957
Showing 1 changed file with 25 additions and 20 deletions.
45 changes: 25 additions & 20 deletions OpenForPSCmdlet/OpenForPSCmdletPackage.cs
Expand Up @@ -12,6 +12,7 @@
using EnvDTE80;
using EnvDTE;
using System.Linq;
using Process = System.Diagnostics.Process;

namespace tanaka_733.OpenForPSCmdlet
{
Expand Down Expand Up @@ -90,6 +91,8 @@ protected override void Initialize()
}
#endregion

private int? processId;

/// <summary>
/// This function is the callback used to execute a command when the a menu item is clicked.
/// See the Initialize method to see how the menu item is associated to this function using
Expand All @@ -106,6 +109,22 @@ private void MenuItemCallback(object sender, EventArgs e)
if (!File.Exists(outputPath))
return;
var dir = Directory.GetParent(outputPath).ToString();

if (processId.HasValue)
{
try
{
var p = Process.GetProcessById(processId.Value);
p.CloseMainWindow();
p.WaitForExit(5000);
}
catch (Exception ex)
{
Debug.WriteLine("Failed to kill previous powershell process.");
Debug.WriteLine(ex);
}
}

try
{
var p = new System.Diagnostics.Process
Expand All @@ -118,36 +137,22 @@ private void MenuItemCallback(object sender, EventArgs e)
}
};
p.Start();
processId = p.Id;

var dteProcess = DTE.Debugger.LocalProcesses.Cast<EnvDTE.Process>().FirstOrDefault(prop => prop.ProcessID == p.Id);
var dteProcess = DTE.Debugger.LocalProcesses.Cast<EnvDTE.Process>().FirstOrDefault(prop => prop.ProcessID == processId);

if (dteProcess != null)
{
dteProcess.Attach();
DTE.Debugger.CurrentProcess = dteProcess;
}
}
catch (Exception)
catch (Exception ex)
{
//TODO log
ActivityLog.LogError("OpenForPSCmdlet", $@"Failed to launch powershell
{ex.Message}
{ex.StackTrace}");
}

// Show a Message Box to prove we were here
//IVsUIShell uiShell = (IVsUIShell)GetService(typeof(SVsUIShell));
//Guid clsid = Guid.Empty;
//int result;
//Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(uiShell.ShowMessageBox(
// 0,
// ref clsid,
// "OpenForPSCmdlet",
// string.Format(CultureInfo.CurrentCulture, "Inside {0}.MenuItemCallback()", this.ToString()),
// string.Empty,
// 0,
// OLEMSGBUTTON.OLEMSGBUTTON_OK,
// OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST,
// OLEMSGICON.OLEMSGICON_INFO,
// 0, // false
// out result));
}

static string GetOutputPath(Project proj)
Expand Down

0 comments on commit 1939957

Please sign in to comment.