1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-10 21:12:56 +01:00
Radarr/NzbDrone/ProcessAttacher.cs

167 lines
3.9 KiB
C#
Raw Normal View History

2010-10-15 09:10:44 +02: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;
2012-05-11 02:04:44 +02:00
using System.Diagnostics;
2011-04-10 04:44:01 +02:00
using System.Runtime.InteropServices;
2010-10-15 09:10:44 +02:00
using EnvDTE80;
2012-05-11 02:04:44 +02:00
using Process = EnvDTE.Process;
2011-04-10 04:44:01 +02:00
using Thread = System.Threading.Thread;
2010-10-15 09:10:44 +02:00
namespace NzbDrone
{
2012-05-11 02:04:44 +02:00
[DebuggerStepThrough]
2010-10-15 09:10:44 +02: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 09:10:44 +02:00
// Get an instance of the currently running Visual Studio IDE.
}
#region private
2010-10-15 09:10:44 +02: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 09:10:44 +02:00
private ProcessAttacher(DTE2 dte, string processName, int waitTimeout)
{
_processName = processName;
_waitTimeout = waitTimeout;
_dte = dte;
2011-04-10 04:44:01 +02:00
_attachTypesMap = new Dictionary<AttachType, string>
{
{AttachType.Managed, "Managed"}
};
2010-10-15 09:10:44 +02:00
}
#endregion
#region private methods
2010-10-15 09:10:44 +02: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 04:44:01 +02:00
Process2 proc = null;
2010-10-15 09:10:44 +02:00
try
{
2011-04-10 04:44:01 +02:00
proc = dbg.GetProcesses(trans, "").Item(_processName) as Process2;
2010-10-15 09:10:44 +02:00
}
2011-03-30 08:18:35 +02:00
catch (Exception)
2010-10-15 09:10:44 +02:00
{
return AttachResult.NotRunning;
2010-10-15 09:10:44 +02:00
}
proc.Attach2(eng);
return AttachResult.Attached;
}
private AttachResult PessimisticAttach(AttachType attachType)
{
2011-10-07 08:57:43 +02:00
var res = Attach(attachType);
2010-10-15 09:10:44 +02:00
2011-10-07 08:57:43 +02:00
var timeout = DateTime.Now.AddSeconds(_waitTimeout);
2010-10-15 09:10:44 +02:00
while (res == AttachResult.NotRunning && timeout > DateTime.Now)
{
res = Attach(attachType);
2011-04-10 04:44:01 +02:00
Thread.Sleep(100);
2010-10-15 09:10:44 +02:00
}
return res;
}
private bool IsBeingDebugged()
{
if (_dte.Debugger.DebuggedProcesses != null)
{
2011-04-10 04:44:01 +02:00
foreach (Process process in _dte.Debugger.DebuggedProcesses)
2010-10-15 09:10:44 +02:00
{
if (process.Name.IndexOf(_processName) != -1)
{
return true;
}
}
}
return false;
}
#endregion
#region public methods
2010-10-15 09:10:44 +02:00
public void OptimisticAttachManaged()
{
Attach(AttachType.Managed);
}
public void PessimisticAttachManaged()
{
PessimisticAttach(AttachType.Managed);
}
#endregion
2011-04-10 04:44:01 +02:00
#region Nested type: AttachResult
private enum AttachResult
{
Attached,
NotRunning,
BeingDebugged
}
#endregion
2011-04-10 04:44:01 +02:00
#region Nested type: AttachType
private enum AttachType
{
Managed,
Native,
ManagedAndNative
}
#endregion
}
}
2010-10-15 09:10:44 +02:00
2011-04-10 04:44:01 +02:00
#endif