Davinci
2005-11-28 17:14:55 UTC
In .NET 2.0 if you use AppDomain.GetCurrentThreadId then you will get a
warning that the call is obsolete and you should use ManagedThreadId
property of the Thread object.
If you were to look at the AppDomain.GetCurrentThreadId() method and
Thread.ManagedThreadId property documentation you will notice that they do
not accomplish the same task. Yes, they both give you a Thread ID but
GetCurrentThreadId() gives you the current running thread when it is called
and the ManagedThreadId gives you the thread Id of the Instance of a
System.Threading.Thread object.
That said you are not getting what you want when you want it if you simply
replaced GetCurrentThreadId with ManagedThreadID.
Allow me to show you how GetCurrentThreadId will be used in a application
but first look at how it is used behind the seen.
private void button1_Click(object sender, EventArgs e)
{
lock (label1)
{
ChangeBtn();
}
}
private void ChangeBtn()
{
lock (label1)
{
Button1.Text = "ok";
}
}
The above code works fine the buttons caption changes from Button1 to
ok with no problems. Why? Because in the method ChangeBtn() the CLR
checked the thread ID of when you last locked the label1 and it was done in
the same thread. So the since the lock was none in the same thread then the
execution will continue and will not deadlock.
Now lets just say I knew that a method could not be executed in a particular
thread and if it did a deadlock would occur I would save my Thread ID that
the code would should not execute with and then test it against the current
thread the method was called.
private void button4_Click(object sender, EventArgs e)
{
if (FSaveLabelThreadID != AppDomain.GetCurrentThreadId())
{
MySemaphore.Enter;
try
{
ChangeBtn();
}
finally
{
MySemaphore.Leave;
}
}
else
throw new Exception("Deadlock will occur");
}
private void ChangeBtn()
{
if (FSaveLabelThreadID != AppDomain.GetCurrentThreadId())
{
MySemaphore.Enter;
try
{
button4.Text = "ok";
}
finally
{
MySemaphore.Leave;
}
}
else
throw new Exception("Deadlock will occur");
}
So my question is how would I replace AppDomain.GetCurrentThreadId() so that
I can find out what thread my code is running under?
Thanks
Davinci
warning that the call is obsolete and you should use ManagedThreadId
property of the Thread object.
If you were to look at the AppDomain.GetCurrentThreadId() method and
Thread.ManagedThreadId property documentation you will notice that they do
not accomplish the same task. Yes, they both give you a Thread ID but
GetCurrentThreadId() gives you the current running thread when it is called
and the ManagedThreadId gives you the thread Id of the Instance of a
System.Threading.Thread object.
That said you are not getting what you want when you want it if you simply
replaced GetCurrentThreadId with ManagedThreadID.
Allow me to show you how GetCurrentThreadId will be used in a application
but first look at how it is used behind the seen.
private void button1_Click(object sender, EventArgs e)
{
lock (label1)
{
ChangeBtn();
}
}
private void ChangeBtn()
{
lock (label1)
{
Button1.Text = "ok";
}
}
The above code works fine the buttons caption changes from Button1 to
ok with no problems. Why? Because in the method ChangeBtn() the CLR
checked the thread ID of when you last locked the label1 and it was done in
the same thread. So the since the lock was none in the same thread then the
execution will continue and will not deadlock.
Now lets just say I knew that a method could not be executed in a particular
thread and if it did a deadlock would occur I would save my Thread ID that
the code would should not execute with and then test it against the current
thread the method was called.
private void button4_Click(object sender, EventArgs e)
{
if (FSaveLabelThreadID != AppDomain.GetCurrentThreadId())
{
MySemaphore.Enter;
try
{
ChangeBtn();
}
finally
{
MySemaphore.Leave;
}
}
else
throw new Exception("Deadlock will occur");
}
private void ChangeBtn()
{
if (FSaveLabelThreadID != AppDomain.GetCurrentThreadId())
{
MySemaphore.Enter;
try
{
button4.Text = "ok";
}
finally
{
MySemaphore.Leave;
}
}
else
throw new Exception("Deadlock will occur");
}
So my question is how would I replace AppDomain.GetCurrentThreadId() so that
I can find out what thread my code is running under?
Thanks
Davinci