Reputation: 15301
My application runs some threads and every thread does something. I want that each thread shows its status in a list box.
So, how a thread can identify itself (by using thread ID or something else) and set its status into a List
?
Upvotes: 0
Views: 184
Reputation: 4744
You could just keep a collection (Array, List, whatever you want) of the Thread you want to monitor. The thread class exposes many properties you may want, including the ThreadState property. From there it is easy to display the informations you want.
This way your working threads will not have to worry about notifying their status.
Upvotes: 1
Reputation: 16032
Every thread has an id. You can access it with
Thread t;
int id = t.ManagedThreadId;
To update a global datastructure with some state i would use a dictionary and use the id as the key. Be aware of locking the access of that data structure:
Dictionary<int, SomeStateClass> threadStates = new Dictionary<int, SomeStateClass>();
public void updateThreadState(int id, SomeStateClass newState) {
lock (threadStates) {
threadStates[id] = newState;
}
}
Upvotes: 1
Reputation: 1225
You could use Invoke
with delegates. Each thread would then be responsible for updating the ListBoxItem it owns.
Upvotes: 1
Reputation: 11957
You can identify managed threads with Thread.CurrentThread.ManagedThreadId. And remember to Invoke calls to your listbox (or use Dispatcher in case you are using WPF), as threads other then UI thread can not access it directly.
Upvotes: 2