I have the following context:
private void btn_Click(object sender, EventArgs e)
{
Thread newThread = new Thread(Step1);
newThread.Start(this);
}
private void Step1(object stateInfo)
{
Register();
// work
Thread newThread = new Thread(Step2);
newThread.Start(this);
}
private void Step2(object stateInfo)
{
Register();
// work
Thread newThread = new Thread(Step3);
newThread.Start(this);
}
private void Step3(object stateInfo)
{
Register();
// work
// finish
}
private void Register()
{
// adds a new entry to a Dictionary,
// where the key is the Thread's ID (System.Threading.Thread.CurrentThread.ManagedThreadId)
// and the value is a resource
// the goal is for a Thread not to register itself twice, or not to change its resource
}
As the comments say, I need for each Thread to save a resource. This should be done only once per Thread and shouldn't be modifiable.
The problem is that, sometimes, the Thread from Step3 has the same ID as the Thread from Step1. I think it makes sense; because by the time it gets to Step3, Step1's Thread is already 'dead' and its ID can be reused.
Do you have any suggestions? Is there something wrong with my approach? Is there a way of obtaining/generating an unique ID for all the Threads that a Process creates?
[Edit 1] Included the .NET 2.0 constraint.