Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I would punt on getting the file name. It can be done, but it is probably not worth the effort. Instead, set the <code>Name</code> property on the <code>Thread</code> to the name of the class that created it.</p> <p>You will be able to see the <code>Name</code> value when inspected with the Visual Studio debugger. If you want to get a list of all managed threads in the current process via code then you will need to create your own thread repository. You cannot map a <code>ProcessThread</code> to a <code>Thread</code> because there is not always a one-to-one relationship between the two.</p> <pre><code>public static class ThreadManager { private List&lt;Thread&gt; threads = new List&lt;Thread&gt;(); public static Thread StartNew(string name, Action action) { var thread = new Thread( () =&gt; { lock (threads) { threads.Add(Thread.CurrentThread); } try { action(); } finally { lock (threads) { threads.Remove(Thread.CurrentThread); } } }); thread.Name = name; thread.Start(); } public static IEnumerable&lt;Thread&gt; ActiveThreads { get { lock (threads) { return new List&lt;Thread&gt;(threads); } } } } </code></pre> <p>And it would be used like this.</p> <pre><code>class SomeClass { public void StartOperation() { string name = typeof(SomeClass).FullName; ThreadManager.StartNew(name, () =&gt; RunOperation()); } } </code></pre> <p><strong>Update:</strong></p> <p>If you are using C# 5.0 or higher you can experiment with the new <a href="http://msdn.microsoft.com/en-us/library/hh534540.aspx" rel="nofollow">Caller Information</a> attributes.</p> <pre><code>class Program { public static void Main() { DoSomething(); } private static void DoSomething() { GetCallerInformation(); } private static void GetCallerInformation( [CallerMemberName] string memberName = "", [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0) { Console.WriteLine("Member Name: " + memberName); Console.WriteLine("File: " + sourceFilePath); Console.WriteLine("Line Number: " + sourceLineNumber.ToString()); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload