472,358 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,358 software developers and data experts.

AppDomain.GetCurrentThreadId() and ManagedThreadId are different

I have an application that use log4net for operational logging. I
store some user data elsewhere and need to tie the two together. To
achieve this I pass the ThreadId across on the user table so I can see
what thread the user was running under and then look in the log4net
table to see what they were up to.

This works fine when I use AppDomain.GetCurrentThreadId() as the
ThreadIds match, but the compiler throws up an obsolete warning and
says to use Threads ManagedThreadId property.

Howerever they return different Id's, in fact ManagedThreadId seems to
always return 1 (???).
Here's some unit testing code that won't work:

Assert.AreEqual(AppDomain.GetCurrentThreadId(),
System.Threading.Thread.CurrentThread.ManagedThrea dId);

So, the question is, what method/property returns the Id which is akin
to the old AppDomain CurrentThreadId? My suspicion is, because of the
unstable nature of AppDomain the answer is there isn't - but what is
the closest (otherwise I will have to stick with AppDomain despite
compiler warnings).

Cheers

Dec 30 '05 #1
1 20017
Peter,

And they almost never will equal, not anymore.

In .NET 2.0, the hosting model allows for the same thread to be used to
process different "logical" threads of execution. Mainly, this was inserted
to allow the CLR to be hosted in SQL Server.

Now, the call to GetCurrentThreadId was a call to the GetCurrentThreadId
function through the P/Invoke layer. If one was to use this as a unique
identifier which is relative to the logical thread of execution, then this
could have an effect on existing code.

As a result, the ManagedThreadId property is recommended, as it will
differentiate between two logical threads, even though they might run on the
same thread.

The reason it returns 1 is that 1 is the first id generated (it uses the
hash codes of the objects, I believe) for the Thread instance you are using.

So, the solution, assuming that you are using the result of
GetCurrentThreadId as an identifier for ^logical^ threads of execution, is
to move to ManagedThreadId.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<pe********@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
I have an application that use log4net for operational logging. I
store some user data elsewhere and need to tie the two together. To
achieve this I pass the ThreadId across on the user table so I can see
what thread the user was running under and then look in the log4net
table to see what they were up to.

This works fine when I use AppDomain.GetCurrentThreadId() as the
ThreadIds match, but the compiler throws up an obsolete warning and
says to use Threads ManagedThreadId property.

Howerever they return different Id's, in fact ManagedThreadId seems to
always return 1 (???).
Here's some unit testing code that won't work:

Assert.AreEqual(AppDomain.GetCurrentThreadId(),
System.Threading.Thread.CurrentThread.ManagedThrea dId);

So, the question is, what method/property returns the Id which is akin
to the old AppDomain CurrentThreadId? My suspicion is, because of the
unstable nature of AppDomain the answer is there isn't - but what is
the closest (otherwise I will have to stick with AppDomain despite
compiler warnings).

Cheers

Dec 30 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Daylor | last post by:
in win32 process , when u create new process,u have new main thread. i know,appDomain r logical procces,that exists in 1 win32 process. the q: is there way to create second appDomain (the...
1
by: MatthewRoberts | last post by:
Howdy All, I am having difficulty with two-way communication across AppDomains in an attempt to dynamically script applications. Everything works as expected, except when using ByRef parameters....
8
by: A. Elamiri | last post by:
Hello, I created a small app which acts as a services manager. I basically drop a DLL in a Services folder and set the frequency through the application for how often do I want the code in the...
2
by: Mochuelo | last post by:
Hi, Is there a non-obsolete alternative to the obsolete GetCurrentThreadID? I just need an ID (ideally, an int) that will distinguish any two threads. Thanks.
16
by: Elad | last post by:
Hi, I have an application that is made up of several executables. I need all these executables to use the same instance of an object. What is the best, most efficient way to approach this? ...
0
by: mathboy2 | last post by:
Hey, I've created a very simple test app to try and fix a problem I'm having. Basically I'm trying to log to a text file from a class in a seperate app domain that I've created. When I first...
1
by: =?Utf-8?B?aGVyYmVydA==?= | last post by:
the .NET 2.0 documentation recommends to use Thread.CurrentThread.ManagedThreadId instead of deprecated AppDomain.GetCurrentThreadId. The VS.2005 debugger's Thread window however displays only...
1
by: Bill Woodruff | last post by:
Visual Studio 2005, .NET FrameWork 2.0, C#, WinForms Application Hi, I've read the recent posts by and to 'Thunderbird' (and learned a lot, thanks, from the usual masters Skeet and Paladino,...
3
by: =?Utf-8?B?S2VuRGV2?= | last post by:
I would like to know if there is a pragma id that I can use to suppress the warning I get (.NET 2.0) for the GetCurrentThreadId. I want to use warnings as errors and I don't want to change the...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
1
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.