System.Threading.Thread replacement in UWP / Windows 10 mobile
Asked Answered
D

4

4

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.

Disarming answered 30/11, 2015 at 14:23 Comment(0)
S
2

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

Singhalese answered 30/11, 2015 at 19:29 Comment(7)
Yeah thanks, I've already seen tasks prior to asking this question. My app has a constant worker thread. From what I've read, tasks are intended for single asynchronous operations. Is there really no way of working with threads?Disarming
Tasks are not for only single async ops. You can use task as threads. Actually underneath Task is the Thread. So just try to switch from threads and use tasks. You can start one task that later can starts other tasks. You do not need to wait till tasks ends, just fire it up and forget, but provide some mechanism for task to finish gracefully. For this you can use Task Cancellation Token.Singhalese
Ray, look at this blog: blog.slaks.net/2013-10-11/threads-vs-tasks It states there: "Since tasks still run on the ThreadPool, they should not be used for long-running operations, since they can still fill up the thread pool and block new work. Instead, Task provides a LongRunning option, which will tell the TaskScheduler to spin up a new thread rather than running on the ThreadPool." Eventually, I need a thread that will stay alive through the app's lifecycle. Perhaps creating the task with LongRunning option is the way to go...Disarming
You misunderstood. I'm not refusing to use them. Thanks for the answer, I'm inspecting this solution combined with what I suggested.Disarming
I've ended up using long running task, as I stated before. Thanks!Disarming
How did you prevent task switches when using await? Everytime i use await Task.Delay it switches.Nkrumah
Benni, not sure I understand the question or it's relevance to the subjectDisarming
M
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

Murderous answered 24/3, 2017 at 16:30 Comment(0)
A
2

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.

Abib answered 8/5, 2017 at 22:55 Comment(1)
unfortunately that one doesn't include ThreadPool (used DedicatedThreadPool with some small changes instead)Ritaritardando
R
0

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?

Reenter answered 13/3, 2017 at 9:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.