473,663 Members | 2,719 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

AppDomain.GetCu rrentThreadId() 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.GetCu rrentThreadId() 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.GetC urrentThreadId( ),
System.Threadin g.Thread.Curren tThread.Managed ThreadId);

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 20211
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 GetCurrentThrea dId was a call to the GetCurrentThrea dId
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
GetCurrentThrea dId 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.co m

<pe********@gma il.com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.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.GetCu rrentThreadId() 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.GetC urrentThreadId( ),
System.Threadin g.Thread.Curren tThread.Managed ThreadId);

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
6197
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 first 1 is the defualt appDomain) with his new ("main") thread ? if not ,what is the easy and clear way to create new thread on the new appDomain ?
1
4108
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. The below explanation is lengthy, but well worth the read. If you can help me, I'd gladly share this code which has greatly helped my development of extensible applications.
8
5085
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 assembly to run (scheduler). I created a seperate AppDomain here is the code: .... AppDomainSetup ads = new AppDomainSetup(); string path =
2
3632
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
10103
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? Thanks a lot!
0
857
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 create the AppDomain I'm able to log. However, I have the requirement that I'll have to tear down the AppDomain and then load it back up again. When I do this I no longer get any logging. Note that the first thing I thought of is that the file was...
1
5633
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 the AppDomain.GetCurrentThreadId. How do I get a Thread.CurrentThread.ManagedThreadId column in the debugger window?
1
4282
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, and others) which involve AppDomains in a remoting scenario, but I think the issue I am working with .... while related ... is sufficiently different to warrant a new thread. I'm also working on a plug-in architecture it's for a WinForms based
3
3789
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 GetCurrentThreadId as we in the testing stages for our product (I have read a few comments about problems just switching to the ManagedThreadId). Does such a pragma id exist, and if so, what is it? Thanks,
0
8345
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8857
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8768
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8633
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7368
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5655
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4181
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4348
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1999
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.