简体   繁体   English

在UWP / Windows 10移动设备中更换System.Threading.Thread

[英]System.Threading.Thread replacement in UWP / Windows 10 mobile

In UWP, how can I access the thread object? 在UWP中,如何访问线程对象? I want to change the name of the main thread and additional thread that I will manage. 我想更改主线程的名称和我将管理的其他线程。

I also want to check if the current thread is the main thread afterwards. 我还想检查当前线程是否是之后的主线程。

I'm targeting to windows 10 mobile. 我的目标是windows 10 mobile。 The following code example works for desktop, but not for phone (getting 'System.DllNotFoundException'): 以下代码示例适用于桌面,但不适用于手机(获取'System.DllNotFoundException'):

[DllImport("kernel32.dll")]
static extern IntPtr GetCurrentThread();

Thanks. 谢谢。

You should use Tasks instead of threads. 您应该使用Tasks而不是线程。 https://msdn.microsoft.com/en-us/library/system.threading.tasks.task%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396 https://msdn.microsoft.com/en-us/library/system.threading.tasks.task%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

As about renaming of threads, can you tell me why do you need to rename main thread and other threads!? 至于重命名线程,你能告诉我为什么你需要重命名主线程和其他线程!?

你应该尝试System.Environment.CurrentManagedThreadId(是的,你只能获得当前的线程ID) https://msdn.microsoft.com/en-us/library/system.environment.currentmanagedthreadid(v= vs.110) .aspx

We've blogged about this at length (as we feel this is an unwelcome start of the fragmentation of .NET Standard) and have published an open source System.Threading.Thread implementation for UWP/UAP 10, available on GitHub and via NuGet . 我们已经详细介绍了这一点(因为我们认为这是.NET Standard碎片化的一个不受欢迎的开始)并且已经发布了一个开源的System.Threading.Thread实现UWP / UAP 10, 可以在GitHubNuGet上获得

I hope this helps. 我希望这有帮助。

In UWP, you should look into using the classes from the Windows.System.Threading namespace . 在UWP中,您应该考虑使用Windows.System.Threading命名空间中的类。 However, you will notice there is a thread pool, but no direct equivalent of the Thread class. 但是,您会注意到有一个线程池,但没有直接等效的Thread类。

Remember that there are reasons why UWP does not allow you to just create a thread and leave it running for ever. 请记住,UWP不允许您只创建一个线程并让它永远运行的原因。 That would make it too easy to drain the battery, but it would also get in the way of app suspension. 这样会使电池耗尽太容易,但也会妨碍应用暂停。

UWP has many mechanisms that allow you to do the kinds of things that on other platforms might be solved by using a dedicated thread. UWP有许多机制允许您通过使用专用线程来完成其他平台上可能解决的问题。 Maybe if you told us why you think you need a thread, we can help you in solving your actual problem? 也许如果你告诉我们为什么你认为你需要一个主题,我们可以帮你解决你的实际问题?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM