23.01.2018 Views

MICROSOFT_PRESS_EBOOK_PROGRAMMING_WINDOWS_8_APPS_WITH_HTML_CSS_AND_JAVASCRIPT_PDF

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Arrays, Vectors, and Other Alternatives<br />

Now that we’ve seen the basic structure of asynchronous methods in WinRT components, let’s see how<br />

we might create an asynchronous variant of the synchronous Convert methods we implemented earlier.<br />

For the purpose of this exercise we’ll just stick with the C# component.<br />

It would be natural with Convert to consider IAsyncAction as the method’s type, because we already<br />

return results in an output array. This would, in fact, be a great choice if we were using types other than<br />

an array. However, arrays present a variety of problems with asynchronous methods. First, although we<br />

can pass the method both the input and output arrays, and the method can do its job and populate that<br />

output array, its contents won’t actually be transferred back across the async task boundary at present.<br />

So the completed handler in the app will be called as it would expect, but the output array passed to the<br />

async method will still be empty.<br />

The next thing we can try is to turn the async action into an operation that produces a result. We<br />

might consider a return type of IAsyncOperation (or an equivalent one using progress), where<br />

the method would create and populate the array it returns. The problem here, however, is that the app<br />

receiving this array wouldn’t know how to release it—clearly some memory was allocated for it, but that<br />

allocation happened inside a component and not inside JavaScript, so there’s no clear rule on what to<br />

do. Because this is a sure fire recipe for memory leaks, returning arrays like this isn’t supported.<br />

An alternative is for the async method to return a specific WinRT collection type (where there are<br />

clear rules for deallocation), such as an IList, which will be converted to a vector in JavaScript<br />

that can also be accessed as an array. (Note that IList is specific to .NET languages; the C++<br />

walkthrough topic shows how to use a vector directly with the concurrent_vector type.) Here’s a simple<br />

example of such a method:<br />

public static IAsyncOperation CreateByteListAsync(int size)<br />

{<br />

var task = Task.Run(() =><br />

{<br />

Byte [] list = new Byte[size];<br />

for (int i = 0; i < size; i++)<br />

{<br />

list[i] = (Byte)(i % 256);<br />

}<br />

return list.ToList();<br />

});<br />

}<br />

return task.AsAsyncOperation();<br />

Applying this approach to the grayscale routine, we get the following ConvertPixelArrayAsync (see<br />

PixelCruncherCS > ConvertGrayscale.cs), where the DoGrayscale is the core code of the routine broken<br />

out into a separate function, the third parameter of which is a periodic callback that we can use to<br />

handle cancellation):<br />

742

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!