Is there a way to check within someObj.someMethod()
if it is being executed on the same thread on which someObj
was created? This could save me a debugging headache later on if I mess up certain concurrency constraints.
The only way to do that is to store the thread-id when you create it. On .NET 4.5:
readonly int ownerThreadId;
public SomeType() {
ownerThreadId = Environment.CurrentManagedThreadId;
}
then check against that same term in someMethod
.
Note that on other framework versions, you might need:
ownerThreadId = Thread.CurrentThread.ManagedThreadId;
instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With