Fowl
Fowl

Reputation: 5128

Detect Attached Profiler

I know you can determine if a debugger is attached by using System.Diagnostics.Debugger.IsAttached, but is there a way of determining if a profiler is attached?

My profiler can't trace tasks through the threadpool so I want to automaticly disable parallelism when profiling.

Upvotes: 4

Views: 774

Answers (3)

Sebastien Pellizzari
Sebastien Pellizzari

Reputation: 1025

For those interested, it seems you can detect if your process is being profiled by the Diagnostics Hub of Visual Studio (at least in Visual Studio 2017) by checking the existence of the DIAGHUB_SESSION_ID environment variable.

Upvotes: 1

leppie
leppie

Reputation: 117330

For the standard profiler interface, an environment variable needs to be set.

I guess you can just check this via the Environment class.

The variable is called COR_ENABLE_PROFILING and if enabled, will be set to 1.

Upvotes: 7

Jeremy Thompson
Jeremy Thompson

Reputation: 65712

Debugger.IsAttached code is included whether debug or release build. And a debugger can be attached to release builds to.

Hence System.Diagnostics.Debugger.IsAttached would return False if no debugger (or profiler) is attached.

Note: I am not sure about SlimTune but Profilers you tend to attach to the process: How to: Attach and Detach the Profiler to Running Processes

Upvotes: 1

Related Questions