Which thread class except System.Threading in C# i can use in windows phone development? [closed]
Is there another c# class for threads, which works faster ?
Based on your comment, you are using the Thread
class in the System.Threading
namespace.
Based on that, none of the classes are going to make the code that executes on the thread any faster; the best you can do is optimize the performance of the mechanism that you use for your situation.
That said, there are some things to be aware of:
- The
Thread
class probably has the most overhead; every time you create one, it creates a new managed thread, which might create a new OS thread. This is not an inconsequential operation. - Using the
ThreadPool
class to schedule operations can be faster in terms of starting your operation, as it keeps a cache of threads already created so you don't have to pay that penalty. The trade-off here is that a) many classes in the .NET framework use theThreadPool
to schedule tasks, and your task will be placed in a queue; there's no way to force it to execute immediately when placed in theThreadPool
. - The
Task
andTask<TResult>
classes in theSystem.Threading.Tasks
namespace run on top of theThreadPool
(this can be configured otherwise, to run with newThread
instances every time, for example), so you gain those benefits, but at the same time, there is overhead for cancellation, continuation, etc, etc. You gain ease-of-use here (much in my opinion), with minimal overhead.
In the end, you will have to measure what is best for your application and apply that solution; like most, there is no one-size-fits-all solution.
For most circumstances using your own instances of the Thread
class is going to be slower than using work items in the ThreadPool
. It would take some extraordinary circumstances for you to find an advantage using the Thread
class directly.
精彩评论