In a VS extension project, I am trying to create a mapping of the process threads, cast as both EnvDTE.Thread (to access the Freeze and Thaw methods), and System.Threading.Thread (to access the ManagedThreadId property).
It should ideally be as follow, but the cast will not compile, saying that it cannot cast from System.Threading.Thread to EnvDTE.Thread.
var threads = new Dictionary<EnvDTE.Thread, System.Threading.Thread>();
foreach (System.Threading.Thread thread in this.dte.Debugger.CurrentProgram.Threads) {
threads.Add((EnvDTE.Thread)thread, thread);
}
How can I force the cast, knowing that it will not throw an exception (unless I am missing something here)?
Edit: it does throw an InvalidCastException.
The compilation error you’re getting is a bit of a red herring.
Debugger.CurrentProgram.Threadsalready returns a collection ofEnvDTE.Threadobjects, so yourforeachwill fail when attempting to cast them toSystem.Threading.Threadsince there is no relation between those classes beyond the name.EnvDTE.Threadhas anIDproperty. Would that do what you want without needing to convert to aSystem.Threading.Thread?