Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
有没有办法检查someObj.someMethod()它是否在创建的同一线程上执行someObj?如果我弄乱了某些并发约束,这可以让我以后在调试时头疼。
someObj.someMethod()
someObj
唯一的方法是在创建线程 ID 时存储它。在 .NET 4.5 上:
readonly int ownerThreadId; public SomeType() { ownerThreadId = Environment.CurrentManagedThreadId; }
然后检查中的相同术语someMethod。
someMethod
请注意,在其他框架版本上,您可能需要:
ownerThreadId = Thread.CurrentThread.ManagedThreadId;
反而。