vsix 实验实例中的事件只工作一次
Events in the vsix experimental instance working only once
我创建了一个 VSIX 项目包并添加了一些事件,这些事件应该在调试器启动时被调用。
[PackageRegistration(UseManagedResourcesOnly = true, AllowsBackgroundLoading = true)]
[InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)] // Info on this package for Help/About
[ProvideAutoLoad(UIContextGuids80.Debugging)]
[Guid(FirstPackage.PackageGuidString)]
public sealed class FirstPackage : AsyncPackage, _dispDebuggerEvents
{
public const string PackageGuidString = "5ed493bb-1bfd-4946-a427-e400ee8653d9";
protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
{
// Garbage Collector ?
await base.InitializeAsync(cancellationToken, progress);
await this.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);
EnvDTE80.DTE2 dte = (EnvDTE80.DTE2)Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(DTE));
DebuggerEvents _debuggerEvents = dte.Events.DebuggerEvents;
_debuggerEvents.OnEnterRunMode += OnEnterRunMode;
_debuggerEvents.OnEnterBreakMode += OnEnterBreakMode;
}
public void OnEnterRunMode(dbgEventReason Reason)
{
// do some stuff
}
public void OnEnterBreakMode(dbgEventReason Reason, ref dbgExecutionAction ExecutionAction)
{
// do some other stuff
}
这是我当前的代码。如果我启动程序,事件将被初始化,并且通过在我的实验实例中启动调试器也会触发。我目前的问题是,它只在我第一次调试实验实例时起作用。
您只需确保 DebuggerEvents 不会超出范围,这样您的事件处理程序就不会被 .NET 垃圾收集器吃掉。
例如:
public sealed class FirstPackage : AsyncPackage, _dispDebuggerEvents
{
public const string PackageGuidString = "5ed493bb-1bfd-4946-a427-e400ee8653d9";
private DebuggerEvents _debuggerEvents;
protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
{
// Garbage Collector ?
await base.InitializeAsync(cancellationToken, progress);
await this.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);
EnvDTE80.DTE2 dte = (EnvDTE80.DTE2)Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(DTE));
_debuggerEvents = dte.Events.DebuggerEvents;
_debuggerEvents.OnEnterRunMode += OnEnterRunMode;
_debuggerEvents.OnEnterBreakMode += OnEnterBreakMode;
}
我创建了一个 VSIX 项目包并添加了一些事件,这些事件应该在调试器启动时被调用。
[PackageRegistration(UseManagedResourcesOnly = true, AllowsBackgroundLoading = true)]
[InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)] // Info on this package for Help/About
[ProvideAutoLoad(UIContextGuids80.Debugging)]
[Guid(FirstPackage.PackageGuidString)]
public sealed class FirstPackage : AsyncPackage, _dispDebuggerEvents
{
public const string PackageGuidString = "5ed493bb-1bfd-4946-a427-e400ee8653d9";
protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
{
// Garbage Collector ?
await base.InitializeAsync(cancellationToken, progress);
await this.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);
EnvDTE80.DTE2 dte = (EnvDTE80.DTE2)Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(DTE));
DebuggerEvents _debuggerEvents = dte.Events.DebuggerEvents;
_debuggerEvents.OnEnterRunMode += OnEnterRunMode;
_debuggerEvents.OnEnterBreakMode += OnEnterBreakMode;
}
public void OnEnterRunMode(dbgEventReason Reason)
{
// do some stuff
}
public void OnEnterBreakMode(dbgEventReason Reason, ref dbgExecutionAction ExecutionAction)
{
// do some other stuff
}
这是我当前的代码。如果我启动程序,事件将被初始化,并且通过在我的实验实例中启动调试器也会触发。我目前的问题是,它只在我第一次调试实验实例时起作用。
您只需确保 DebuggerEvents 不会超出范围,这样您的事件处理程序就不会被 .NET 垃圾收集器吃掉。
例如:
public sealed class FirstPackage : AsyncPackage, _dispDebuggerEvents
{
public const string PackageGuidString = "5ed493bb-1bfd-4946-a427-e400ee8653d9";
private DebuggerEvents _debuggerEvents;
protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
{
// Garbage Collector ?
await base.InitializeAsync(cancellationToken, progress);
await this.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);
EnvDTE80.DTE2 dte = (EnvDTE80.DTE2)Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(DTE));
_debuggerEvents = dte.Events.DebuggerEvents;
_debuggerEvents.OnEnterRunMode += OnEnterRunMode;
_debuggerEvents.OnEnterBreakMode += OnEnterBreakMode;
}