Number of Threads in Monotouch application

Viewed 1274

I'm using Task and TaskCompletionSource code in my application in scenarios that are frequently invoked, such as downloading a images from the Internet asynchronously from a 'scrolling table view'. This allows me to write async/await code without touching the UI thread for downloading/caching operations.

e.g.:

public override Task<object> GetCachedImage (string key)
    {
        UIImage inMemoryImage = sdImageCache.ImageFromMemoryCache (key);

        //
        // Return synchronously since the image was found in the memory cache.
        if (inMemoryImage != null) {
            return Task.FromResult ((object)inMemoryImage);
        }

        TaskCompletionSource<object> tsc = new TaskCompletionSource<object> ();

        //
        // Query the disk cache asynchronously, invoking the result asynchronously.
        sdImageCache.QueryDiskCache (key, (image, cacheType) => {
            tsc.TrySetResult (image);
        });

        return tsc.Task;
    }

The GetCachedImage is invoked multiple times because a table view may have lots of images to be downloaded and the user may scroll the table view as well. The Task itself does not take too long to be performed (in some cases the result is returned synchronously), so I'd expect that the system create a lot of threads but also REUSE them. However I'm seeing in the console the following output:

Thread finished: <Thread Pool> #149

The number of threads always get bigger and I'm worried my application is creating too many threads and may get stuck because of that after a long period of usage. What does the Thread finished: <Thread Pool> #149 mean? Are threads being created and destroyed? Are threads being reused? Does my application have #149 live threads? Can (should) I limit the max number of threads?


EDIT

As suggested by @usr I ran my application again and stopped the debugger to see how many threads were there, see the screenshots: Debugger threads Debugger console

Looks like 38 threads were created, but some of them were destroyed, I am right? Does that mean that the Thread finished: <Thread Pool> #... message will always appear with a bigger number as long as the application is running? Why aren't threads re-used ?

1 Answers
Related