System.Threading.Thread replacement in UWP / Windows 10 mobile

♀尐吖头ヾ 提交于 2019-12-06 06:50:09

问题


In UWP, how can I access the thread object? 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. The following code example works for desktop, but not for phone (getting 'System.DllNotFoundException'):

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

Thanks.


回答1:


You should use Tasks instead of threads. 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!?




回答2:


You should try System.Environment.CurrentManagedThreadId (yes, you can get only current thread id) https://msdn.microsoft.com/en-us/library/system.environment.currentmanagedthreadid(v=vs.110).aspx




回答3:


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.

I hope this helps.




回答4:


In UWP, you should look into using the classes from the Windows.System.Threading namespace. However, you will notice there is a thread pool, but no direct equivalent of the Thread class.

Remember that there are reasons why UWP does not allow you to just create a thread and leave it running for ever. 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. Maybe if you told us why you think you need a thread, we can help you in solving your actual problem?



来源:https://stackoverflow.com/questions/34000838/system-threading-thread-replacement-in-uwp-windows-10-mobile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!