Getting current thread ID

  • Thread starter Thread starter Claire
  • Start date Start date
C

Claire

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
 
Claire said:
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()
 
Claire said:
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;
}
 
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();
 
Hi,

You can use native API, it is very simple:
put following declaration:
[DllImport("kernel32")]
private static extern int GetCurrentThreadId();

and then use in any place as regular
int threadID = GetCurrentThreadId();

Hope this helps,
Zabuti Maxim.
 
Back
Top