By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,110 Members | 1,535 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,110 IT Pros & Developers. It's quick & easy.

Getting current thread ID

P: n/a
Ive a static logging routine that's called by all my objects in the
application.
Ive several threads running in my application and Id like the log file to
show the thread ID so I can trace a single thread if I need to.
Ive had a look through system.threading.thread.currentthread
methods/properties but nothing immediately strikes me, how can I obtain a
value for the ID or similar?

thanks
Claire
Nov 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Claire wrote:
Ive a static logging routine that's called by all my objects in the
application.
Ive several threads running in my application and Id like the log file to
show the thread ID so I can trace a single thread if I need to.
Ive had a look through system.threading.thread.currentthread
methods/properties but nothing immediately strikes me, how can I obtain a
value for the ID or similar?

thanks
Claire

i wonder if it is:

system.threading.thread.currentthread.GetDomainID( )
Nov 17 '05 #2

P: n/a
Claire wrote:
Ive a static logging routine that's called by all my objects in the
application.
Ive several threads running in my application and Id like the log file to
show the thread ID so I can trace a single thread if I need to.
Ive had a look through system.threading.thread.currentthread
methods/properties but nothing immediately strikes me, how can I obtain a
value for the ID or similar?

thanks
Claire


i'v done this like that (plus an identation...)

public static void write(string textToAdd, int pm)
{
int indent;

if (!threads.ContainsKey(Thread.CurrentThread))
{
threads.Add(Thread.CurrentThread, 0);
indent = 0;
}
else
{
indent = (int)threads[Thread.CurrentThread];
}

// séparation des threads
if (lastThread != Thread.CurrentThread)

swFromFile.WriteLine("---------------------------------------------------------------------------------------
"+Thread.CurrentThread.Name);

// date
swFromFile.Write(DateTime.Now.ToLongTimeString()+" ");

// indentation
if (pm == -1)
indent--;
for (int i=0; i<indent; i++)
{
swFromFile.Write("\t");
}

if (pm == -1)
indent++;
threads[Thread.CurrentThread] = indent+pm;
// texte
swFromFile.WriteLine(textToAdd);
swFromFile.Flush();

lastThread = Thread.CurrentThread;
}
Nov 17 '05 #3

P: n/a
i wonder if it is:

system.threading.thread.currentthread.GetDomainID( )


Nope, don't think so. That just keeps returning "1" to me
;o(
Nov 17 '05 #4

P: n/a
Thanks for your help Herbert,
I believe it's as follows but,anyone, please correct me if I'm wrong.
int id = System.AppDomain.GetCurrentThreadId();
Nov 17 '05 #5

P: n/a
Thanks z
I tested the two and they both returned the same value, so I'll stick with
dot nets implementation.
Nov 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.