Relationship between a started thread and ProcessThread?

M

Mike Brown

I have created threads in a low level part of my application. When the
application goes to close I use the currentProcess.Threads to get the
current threads associated with my application. The problem is that I can't
identify the threads that were created by lower level parts of the program.
I cannot access the original thread or its name. I can only get the ID from
ProcessThread and when I create the thread I don't have an ID.

What is the relationship between ProcessThread and Thread. How can I get
the thread information from ProcessThread?

OR

How can I determine which threads are mine?

Thanks,

Mike

See Code Below:

Here is the thread creation code:
EMailSender emailSender = new EMailSender(emailSendInfo, emailAddresses);

System.Threading.ThreadStart threadStart = new
System.Threading.ThreadStart(emailSender.Send);

System.Threading.Thread threadMail = new
System.Threading.Thread(threadStart);

threadMail.Name = "Workflow E-Mail Notification Process";

threadMail.Start();



Here is the thread code when the application closes.

private ProcessThreadCollection GetRunningThreads()

{

Process processCurrent = Process.GetCurrentProcess();

if (processCurrent == null)

{

return null;

}

return processCurrent.Threads;

}



private void DisplayThreadsRunning()

{

ProcessThreadCollection processThreads = GetRunningThreads();

if (processThreads != null)

{

string message = string.Format("blah blah cannot be closed, there are {0}
Designer threads still running and must complete first.\n\nThreads
Running:\n", processThreads.Count);

foreach (ProcessThread processThread in processThreads)

{

somehow assemble meaningful thread information like the name for display.

I also get other threads used by system (clr probably created). I can't
distinguish anything meaningful here. processThread.ID doesn't help me
unless I can query the thread with it some how.

}

display running thread info including descriptive names to the user.

}

}
 
G

Guest

One way to achieve this is to store all the thread objects in the list which
you have created programatically.

This is not a ideal way but atleast you will be able to distinguish between
your threads and CLR threads.
 
S

stand__sure

it'd be a bit cleaner to wrap each thread in a class which stores the thread
id in a member variable or which has a routine for closing. the array
approach does work, but it can be a bit daunting to keep track of which
thread is which by index (defining a constant to refer to the index is a
workaround)
 
M

Mike Brown

I thought of managing my created thread list. But the problem is that I
don't have a way to identify which thread is mine when I query it through
the diagnostics call (see code below).

Thread has no ID, but has a name.
ProcessThread has an ID but no name.

How do I identify my threads I created when I query the child treads?

This is my a large part of my problem is that I can't figure out the
relationship between Thread and ProcessThread. You would think that I would
have a thread ID in each one so that I could Identify it. Thread IDs can
also be tricky because they are re-used. When a thread completes and
another starts the new thread may get the ID of a previously run thread that
completed. This further complicates the issue.

Thanks,

Mike
 

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