c# - Get the module/File name, where the thread was created? -


i used below code list threads in running process.

process p=process.getcurrentprocess(); var threads=p.thread; 

but requirement know file name or module name, thread created.

please guide me achieve requirements.

i punt on getting file name. can done, not worth effort. instead, set name property on thread name of class created it.

you able see name value when inspected visual studio debugger. if want list of managed threads in current process via code need create own thread repository. cannot map processthread thread because there not one-to-one relationship between two.

public static class threadmanager {   private list<thread> threads = new list<thread>();    public static thread startnew(string name, action action)   {     var thread = new thread(       () =>       {         lock (threads)         {           threads.add(thread.currentthread);         }         try         {           action();         }                 {           lock (threads)           {             threads.remove(thread.currentthread);           }         }       });     thread.name = name;     thread.start();   }    public static ienumerable<thread> activethreads   {          {        lock (threads)       {         return new list<thread>(threads);        }     }   } } 

and used this.

class someclass {   public void startoperation()   {     string name = typeof(someclass).fullname;     threadmanager.startnew(name, () => runoperation());   } } 

update:

if using c# 5.0 or higher can experiment new caller information attributes.

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());   } } 

Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -