[英]Check if thread is main thread
I'm running some tests on our large app, and I see sometimes the UI thread doing stuff it was not supposed to, like DB/network/etc. 我正在我们的大型应用程序上运行一些测试,有时会看到UI线程执行了原本不应该做的事情,例如DB / network / etc。
I know of IMvxMainThreadDispatcher.RequestMainThreadAction
, but I would like something like: 我知道IMvxMainThreadDispatcher.RequestMainThreadAction
,但是我想要这样的东西:
public bool IsMainThread()
Which would return true if current thread is the main thread. 如果当前线程是主线程,则返回true。
Before I go and create a PR for this code, I just wanted to double check if something similar is not yet available in another way? 在为该代码创建PR之前,我只想仔细检查是否还没有其他类似方法?
Thank you. 谢谢。
Finally I've found something that can help me here. 终于,我找到了一些可以帮助我的东西。
DebugEx.Assert(Environment.CurrentManagedThreadId != 1);
On a PCL library, where we do not have Threading.CurrentThread, finding out about the value on Environment
just helped me find a pesky deadlock we were searching for a long time. 在一个没有Threading.CurrentThread的PCL库中,了解Environment
的值只是帮助我找到了我们长期寻找的讨厌的僵局。 Usually 1 is the UI Thread, but to be sure, put a BP on any UI thread code and double check the value. 通常,UI线程为1,但是可以肯定的是,将BP放在任何UI线程代码上,然后仔细检查该值。
Also, my Assert is below: 另外,我的断言如下:
// The [DebuggerHiddenAttribute] makes debugger stop
// on the actual DebugEx.Assert(...) call, rather than inside
// the DebugEx.Assert(...) method itself.
[DebuggerHiddenAttribute]
public static void Assert(bool condition)
{
if (Debugger.IsAttached)
{
if (!condition)
{
Debugger.Break();
}
}
else
{
Debug.Assert(condition);
}
}
Hope this helps someone else out there. 希望这可以帮助其他人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.