Threading in the .NET Compact Framework

The .NET Compact Framework supports core threading functionality but does not support the following features in the full .NET Framework:

  • Access to the compressed stack on the current thread.

  • The managed representation of the Win32 OVERLAPPED structure.

  • Classes that manage the execution context.

  • Using semaphores.

  • Thread-state attributes.

  • Most wait handle operations except for getting the handle.

The .NET Compact Framework supports using the ThreadStart delegate for use with the Thread.Start method and supports using the TimerCallback delegate with a Timer.

You can specify time-out parameters by using the following threading methods:

Changes in Thread Pools

In the .NET Compact Framework version 1.0, the default maximum number of threads in a thread pool is 256 threads with a stack size of 64 KB. In the .NET Compact Framework version 2.0 and later versions, this limit is reduced to a maximum of 25 threads with a stack size of 128 KB, which more closely matches the functionality of the full .NET Framework.

An HTTP request will fail and throw an exception if no thread is available in the thread pool. The following table shows the main exceptions in this situation.

Exception

Condition

OutOfMemoryException

Unable to queue the user's callback.

WebException

Not enough threads in the thread pool to complete the operation.

You can avoid running out of threads by reducing the number of concurrent Web requests, or by increasing the maximum allowed threads in the thread pool. The .NET Compact Framework 2.0 supports the SetMaxThreads method. Specify its parameters as described in the following table.

Parameter

Description

workerThreads

The maximum number of worker threads in the thread pool. This can be any value.

completionPortThreads

The maximum number of asynchronous threads in the thread pool. The .NET Compact Framework currently ignores this value, but it must be set between 1 and 1,000. For future compatibility, 500 is recommended because it is the default value for the full .NET Framework.

For devices running .NET Compact Framework 1.0, you can decrease the maximum allowed threads in the thread pool by changing a registry setting. Set MaxThreads in CFROOT\ThreadPool key to the desired value. Note that this registry key is not used by later versions of .NET Compact Framework.

See Also

Reference

System.Threading

Other Resources

Application Development in the .NET Compact Framework