Radarr/NzbDrone/ProcessAttacher.cs

166 lines
3.9 KiB
C#
Raw Normal View History

2010-10-15 07:10:44 +00:00
/* SOURCE: http://lazy.codeplex.com/
* File: http://lazy.codeplex.com/SourceControl/changeset/view/55373#307770
* Author: pablito900
* Licence: GNU General Public License version 2 (GPLv2)
*/
#if DEBUG
using System;
using System.Collections.Generic;
2011-04-10 02:44:01 +00:00
using System.Runtime.InteropServices;
using EnvDTE;
2010-10-15 07:10:44 +00:00
using EnvDTE80;
2011-04-10 02:44:01 +00:00
using Thread = System.Threading.Thread;
2010-10-15 07:10:44 +00:00
namespace NzbDrone
{
//[DebuggerStepThrough]
2010-10-15 07:10:44 +00:00
public class ProcessAttacher
{
public static void Attach()
{
DTE2 dte2;
try
{
dte2 = (DTE2)Marshal.GetActiveObject("VisualStudio.DTE.10.0");
}
catch (Exception e)
{
dte2 = (DTE2)Marshal.GetActiveObject("VisualStudio.DTE.11.0");
}
var pa = new ProcessAttacher(dte2, "iisexpress", 10);
pa.PessimisticAttachManaged();
2010-10-15 07:10:44 +00:00
// Get an instance of the currently running Visual Studio IDE.
}
#region private
2010-10-15 07:10:44 +00:00
private readonly Dictionary<AttachType, string> _attachTypesMap;
private readonly DTE2 _dte;
private readonly string _processName;
private readonly int _waitTimeout;
#endregion
#region ctor
2010-10-15 07:10:44 +00:00
private ProcessAttacher(DTE2 dte, string processName, int waitTimeout)
{
_processName = processName;
_waitTimeout = waitTimeout;
_dte = dte;
2011-04-10 02:44:01 +00:00
_attachTypesMap = new Dictionary<AttachType, string>
{
{AttachType.Managed, "Managed"}
};
2010-10-15 07:10:44 +00:00
}
#endregion
#region private methods
2010-10-15 07:10:44 +00:00
private AttachResult Attach(AttachType attachType)
{
string engine = _attachTypesMap[attachType];
if (IsBeingDebugged())
{
return AttachResult.BeingDebugged;
}
var dbg = _dte.Debugger as Debugger2;
var trans = dbg.Transports.Item("Default");
var eng = trans.Engines.Item(engine);
2011-04-10 02:44:01 +00:00
Process2 proc = null;
2010-10-15 07:10:44 +00:00
try
{
2011-04-10 02:44:01 +00:00
proc = dbg.GetProcesses(trans, "").Item(_processName) as Process2;
2010-10-15 07:10:44 +00:00
}
2011-03-30 06:18:35 +00:00
catch (Exception)
2010-10-15 07:10:44 +00:00
{
return AttachResult.NotRunning;
2010-10-15 07:10:44 +00:00
}
proc.Attach2(eng);
return AttachResult.Attached;
}
private AttachResult PessimisticAttach(AttachType attachType)
{
2011-10-07 06:57:43 +00:00
var res = Attach(attachType);
2010-10-15 07:10:44 +00:00
2011-10-07 06:57:43 +00:00
var timeout = DateTime.Now.AddSeconds(_waitTimeout);
2010-10-15 07:10:44 +00:00
while (res == AttachResult.NotRunning && timeout > DateTime.Now)
{
res = Attach(attachType);
2011-04-10 02:44:01 +00:00
Thread.Sleep(100);
2010-10-15 07:10:44 +00:00
}
return res;
}
private bool IsBeingDebugged()
{
if (_dte.Debugger.DebuggedProcesses != null)
{
2011-04-10 02:44:01 +00:00
foreach (Process process in _dte.Debugger.DebuggedProcesses)
2010-10-15 07:10:44 +00:00
{
if (process.Name.IndexOf(_processName) != -1)
{
return true;
}
}
}
return false;
}
#endregion
#region public methods
2010-10-15 07:10:44 +00:00
public void OptimisticAttachManaged()
{
Attach(AttachType.Managed);
}
public void PessimisticAttachManaged()
{
PessimisticAttach(AttachType.Managed);
}
#endregion
2011-04-10 02:44:01 +00:00
#region Nested type: AttachResult
private enum AttachResult
{
Attached,
NotRunning,
BeingDebugged
}
#endregion
2011-04-10 02:44:01 +00:00
#region Nested type: AttachType
private enum AttachType
{
Managed,
Native,
ManagedAndNative
}
#endregion
}
}
2010-10-15 07:10:44 +00:00
2011-04-10 02:44:01 +00:00
#endif