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.

Choosing a Mixed Language Approach (and Web Workers)<br />

There are many reasons to take a mixed language approach in your app, which can again include any<br />

number of WinRT components written in C#, Visual Basic (hereinafter just VB), and/or C++:<br />

• You can accomplish certain tasks faster with higher performance code. This can reduce memory<br />

overhead and also consume less CPU cycles and power, an important consideration for<br />

background tasks for which a CPU quota is enforced—you might get much more done in 2 CPU<br />

seconds in C++ than with JavaScript, because there’s no language engine involved.<br />

• C#, Visual Basic, and C++ have access to a sizable collection of additional APIs that are not<br />

available to JavaScript. These include .NET, Win32, and COM (Component Object Model) APIs,<br />

including the non-UI features of DirectX such as XAudio and Xinput. We’ll see a number of<br />

examples in the “Access to Additional APIs” section later in this chapter.<br />

• Access to other APIs might also be necessary for utilizing third-party .NET/Win32/COM libraries<br />

and also gives you the ability to reuse existing code that you might have in C#, VB, or C++. The<br />

Developing Bing Maps Trip Optimizer, a Windows Store app in JavaScript and C++ topic shows a<br />

complete scenario along these lines, specifically that of migrating an ActiveX control to a WinRT<br />

component so that it can be used from an app, because ActiveX isn’t directly supported. (We<br />

won’t cover this scenario further in this chapter.)<br />

• It can be easier to work with routines involving many async operations by using the await<br />

keyword in C# and Visual Basic, because the structure is much cleaner than with promises. An<br />

example of this can be found in the Here My Am! app of Chapter 17, “Apps for Everyone,” where<br />

the transcodeImage function written in JavaScript for Chapter 13 is rewritten in C# (see<br />

Utilities.ImageConverter.TranscodeImageAsync in the Utilities project).<br />

• A WinRT component written in C++ is more effective at obfuscating sensitive code than<br />

JavaScript and .NET languages. Although it won’t obfuscate the interface to that component, its<br />

internal workings are more painstaking to reverse-engineer.<br />

• A WinRT component is the best way to write a non-UI library that other developers can use in<br />

their chosen language or that you can just use in a variety of your own projects, like the<br />

Notifications Extensions library we saw in earlier chapters. In this context, see How to: Create a<br />

software development kit, which includes details how the component should be structured to<br />

integrate with Visual Studio.<br />

• Although you can use web workers in JavaScript to execute code on different threads, a WinRT<br />

component can be much more efficient for custom asynchronous APIs. Other languages can also<br />

make certain tasks more productive, such as using language-integrated queries (LINQ) from<br />

C#/VB, creating parallel loops in C#/C++, using C++ Accelerated Massive Parallelism (AMP) to<br />

take advantage of the GPU, and so on.<br />

718

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

Saved successfully!

Ooh no, something went wrong!