473,386 Members | 1,644 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 20191
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.