Listing of available threads

D

Darian

Is there a way to find all the thread names that are running in a
project?

For example, if I have 5 threads T1, T2, T3, T4, T5...and T2, T4, and
T5 are running...I want to be able to know that T2, T4 and T5 are
already running.

Thanks,
Darian
 
T

Tom Shelton

Is there a way to find all the thread names that are running in a
project?

For example, if I have 5 threads T1, T2, T3, T4, T5...and T2, T4, and
T5 are running...I want to be able to know that T2, T4 and T5 are
already running.

Thanks,
Darian

You can use the System.Diagnostics.Process class's Threads member to access
your process threads - You won't be able to get the name from there
though...
 
C

Cor Ligthert

Darian,

Do it like most people, just set a boolean array for it and set that to true
something like
private threadrunnunning(5) as boolean

threadrunning(1) = true
if threadrunning(1) = true then

That gives you in my opinion real much more performance than checking it
with any other method.

Just my thought,

Cor

"Darian"
 
S

Shiva

You may use Process.Threads property. On the returned collection, use the
ThreadState property on individual elements to filter out running threads
(for ThreadState.Running).

Hope this is what you are looking for.

Is there a way to find all the thread names that are running in a
project?

For example, if I have 5 threads T1, T2, T3, T4, T5...and T2, T4, and
T5 are running...I want to be able to know that T2, T4 and T5 are
already running.

Thanks,
Darian
 
H

Herfried K. Wagner [MVP]

* Tom Shelton said:
You can use the System.Diagnostics.Process class's Threads member to access
your process threads - You won't be able to get the name from there
though...

This property will give you the number of unmanaged threads, not the
managed threads. So there might be some differences in the results.
 
T

Tom Shelton

This property will give you the number of unmanaged threads, not the
managed threads. So there might be some differences in the results.

Really... I don't see that in the docs. Are you sure about this? I
guess you learn something new every day :)
 
H

Herfried K. Wagner [MVP]

* Tom Shelton said:
Really... I don't see that in the docs. Are you sure about this? I
guess you learn something new every day :)

I never checked it, but I assume that this can be the case ("operating
system threads"):

"
An array of type 'ProcessThread' representing the operating system threads
currently running in the associated process.
"

When starting a new thread using PInvoke inside your .NET application,
then this thread would be listed in 'Threads', but it would not be a
managed thread. If you are using 3rd party components, another
component may start an unmanaged thread.
 
T

Tom Shelton

I never checked it, but I assume that this can be the case ("operating
system threads"):

"
An array of type 'ProcessThread' representing the operating system threads
currently running in the associated process.
"

When starting a new thread using PInvoke inside your .NET application,
then this thread would be listed in 'Threads', but it would not be a
managed thread. If you are using 3rd party components, another
component may start an unmanaged thread.

I just little test of this... Just to find out. Here is the C# code:

using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Diagnostics;

namespace ConsoleApplication17
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Process p = Process.GetCurrentProcess ();

Console.WriteLine ("Intitial: " + p.Threads.Count);
Thread[] threads = new Thread[10];
for (int i = 0; i < threads.Length; i++)
{
threads = new Thread (new ThreadStart
(Class1.ThreadFunc));
threads.Start ();
}


Thread.Sleep (2000);
Console.WriteLine ("===============================");

p = Process.GetCurrentProcess ();
Console.WriteLine ("New: " + p.Threads.Count);
foreach (ProcessThread pthread in p.Threads)
{
Console.WriteLine (pthread.Id);
}
}

static void ThreadFunc()
{
Console.WriteLine ("Child: " + Class1.GetCurrentThreadId
());
Thread.Sleep (new TimeSpan (0, 0, 0, 20, 0));
}

[DllImport ("kernel32")]
static extern IntPtr GetCurrentThreadId ();
}
}

It does indeed include the managed threads... Though, I did find out
that it doen't update dyanmically. The process instance is a snapshot
of the current state of the application, so if threads are started
after you get the instance, then the will not show up in the Threads
collection.
 
H

Herfried K. Wagner [MVP]

Tom,

* Tom Shelton said:
I never checked it, but I assume that this can be the case ("operating
system threads"):

"
An array of type 'ProcessThread' representing the operating system threads
currently running in the associated process.
"

When starting a new thread using PInvoke inside your .NET application,
then this thread would be listed in 'Threads', but it would not be a
managed thread. If you are using 3rd party components, another
component may start an unmanaged thread.

I just little test of this... Just to find out. Here is the C# code:

using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Diagnostics;

namespace ConsoleApplication17
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Process p = Process.GetCurrentProcess ();

Console.WriteLine ("Intitial: " + p.Threads.Count);
Thread[] threads = new Thread[10];
for (int i = 0; i < threads.Length; i++)
{
threads = new Thread (new ThreadStart
(Class1.ThreadFunc));
threads.Start ();
}


Thread.Sleep (2000);
Console.WriteLine ("===============================");

p = Process.GetCurrentProcess ();
Console.WriteLine ("New: " + p.Threads.Count);
foreach (ProcessThread pthread in p.Threads)
{
Console.WriteLine (pthread.Id);
}
}

static void ThreadFunc()
{
Console.WriteLine ("Child: " + Class1.GetCurrentThreadId
());
Thread.Sleep (new TimeSpan (0, 0, 0, 20, 0));
}

[DllImport ("kernel32")]
static extern IntPtr GetCurrentThreadId ();
}
}

It does indeed include the managed threads... Though, I did find out
that it doen't update dyanmically. The process instance is a snapshot
of the current state of the application, so if threads are started
after you get the instance, then the will not show up in the Threads
collection.


Thanks for investigating. I agree that managed threads are listed in
'Threads', but there is neither a guarantee that all threads listed in
'Threads' have been started in your code (that means, that they are
"managed" threads), nor is, as you say, 'Threads' an active snapshot of
the threads belonging to the process.

Consequently I would set up my own datastructure and keep references to
the threads that are alive there.
 
T

Tom Shelton

Tom,

* Tom Shelton said:
Is there a way to find all the thread names that are running in a
project?

For example, if I have 5 threads T1, T2, T3, T4, T5...and T2, T4, and
T5 are running...I want to be able to know that T2, T4 and T5 are
already running.

You can use the System.Diagnostics.Process class's Threads member to access
your process threads - You won't be able to get the name from there
though...

This property will give you the number of unmanaged threads, not the
managed threads. So there might be some differences in the results.


Really... I don't see that in the docs. Are you sure about this? I
guess you learn something new every day :)

I never checked it, but I assume that this can be the case ("operating
system threads"):

"
An array of type 'ProcessThread' representing the operating system threads
currently running in the associated process.
"

When starting a new thread using PInvoke inside your .NET application,
then this thread would be listed in 'Threads', but it would not be a
managed thread. If you are using 3rd party components, another
component may start an unmanaged thread.

I just little test of this... Just to find out. Here is the C# code:

using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Diagnostics;

namespace ConsoleApplication17
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Process p = Process.GetCurrentProcess ();

Console.WriteLine ("Intitial: " + p.Threads.Count);
Thread[] threads = new Thread[10];
for (int i = 0; i < threads.Length; i++)
{
threads = new Thread (new ThreadStart
(Class1.ThreadFunc));
threads.Start ();
}


Thread.Sleep (2000);
Console.WriteLine ("===============================");

p = Process.GetCurrentProcess ();
Console.WriteLine ("New: " + p.Threads.Count);
foreach (ProcessThread pthread in p.Threads)
{
Console.WriteLine (pthread.Id);
}
}

static void ThreadFunc()
{
Console.WriteLine ("Child: " + Class1.GetCurrentThreadId
());
Thread.Sleep (new TimeSpan (0, 0, 0, 20, 0));
}

[DllImport ("kernel32")]
static extern IntPtr GetCurrentThreadId ();
}
}

It does indeed include the managed threads... Though, I did find out
that it doen't update dyanmically. The process instance is a snapshot
of the current state of the application, so if threads are started
after you get the instance, then the will not show up in the Threads
collection.


Thanks for investigating. I agree that managed threads are listed in
'Threads', but there is neither a guarantee that all threads listed in
'Threads' have been started in your code (that means, that they are
"managed" threads), nor is, as you say, 'Threads' an active snapshot of
the threads belonging to the process.

Consequently I would set up my own datastructure and keep references to
the threads that are alive there.


I agree... I would probably use an arraylist or array (depending) to
actually keep a reference to the thread. It was just something that struck
me when I first answered.
 
J

Jay B. Harlow [MVP - Outlook]

Darian,
In addition to the other comments.

Is there a special relationship between your name of the thread & the
thread?

If there is I would store each thread instance in its own variable.

Dim T1, T2, T3, T4, T5 As Thread

When I created the thread I would set the above thread variable, when I
terminated the thread I would clear the above variable.

If there is no special relationship, I would probably use a HashTable.

Of course this assumes you know when the threads start & stop.

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top