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.
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!?
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
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.
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