c# - Thread Renaming -
In Java, it is possible to rename the thread. This is not in .NET. The reason for this is that the name is a property that writes- once in the thread class:
public string name {get {return.m_Name; } [Host Protection (Security Action.LinkDemand, ExternalTrading = true)] Set {Lock (this) {if (this.m_name! = Null) {New Invalid Operation Expansion (Environmental GETResourceString ("InvalidOperation_WriteOnce")); } This.m_Name = Value; InformThreadNameChangeEx (this, this.m_Name); Given the fact that Java is allowed to rename the thread and use the built-in thread structure, both the platforms are OS-supplied, I think I can actually change the name in the thread C # I avoid a certain methodology which i) do not care about me or b) do not use at all Do you have any idea why changing the name of a thread once The registration? Any name changes, does any idea break up?
I have tried a test where I change the name of the thread:
var t1 = new thread (testmistm); T1.Name = "abc"; T1.Start (); T1.GetType (). GetField ("m_Name", BindingFlag.Instance; BindingFlag NonPolic). SetsValue (T1, "DEF"); T1.GetType (). GetMethod ("InformThreadNameChangeEx", BindingFlag NonPolicy | Binding Flags.Static). Invoke (T1, New Object [] {T1, T 1.Name}); The result is that the name has actually been changed and it appears on other codes that use this thread. The background for this is that I need to log into things that thread and the logging library that I use (log4net) uses the thread name, to specify which thread works . thank you in advanced. Edit: Please stop suggesting the obvious things! I know how to name a thread in the beginning, if I am asking how to rename it. Why do I need to do this, that thread will be used again and it can be used by another component and I show that if and when it is entering Instead of a normal number, there should be a specific thread name.
I analyzed that operation and the only code in that BCL that I saw (or more accurate Nikolaos ) Which uses thread.name
getter to call for the RegisterClassEx
API in User32.dll. In the Thread
class, only the m_Name
refers to the member in the name
getter and setter. I suspect that it is safe to rename the thread in the manner you have adopted except that I will change my code to get a lock on the same code that threads Name
Fortunately this is none other than the thread
example, so it is easy to do.
var t1 = New Testament (TestMethod); T1.Name = "abc"; T1.Start (); Lock (T1) {t1.GetType () GetField ("m_Name", BindingFlag.Instance | Compulsive Flag Nonpolic) SetValue (T1, "DEF"); T1.GetType () GetMethod ("InformThreadNameChangeEx", BindingFlags.NonPublic | BindingFlags.Static). Invite (T1, New Object [] {T1, T 1.Name}); } Another thing to note is that you may have problems changing code access protection and private members, depending on which level of application Believe in Obviously this did not come in play with your test, but it is worth mentioning here.
Comments
Post a Comment