310 likes | 439 Views
Building mixed-language apps. Alan Ludwig Senior Software Development Engineer 4-100. The Windows Runtime makes it possible for you to write great apps using more than one language. The Windows Runtime (WinRT). APIs. Cross-language i nfrastructure. A version of Windows.
E N D
Building mixed-language apps Alan Ludwig Senior Software Development Engineer 4-100
The Windows Runtime makes it possible for you to write great apps using more than one language
The Windows Runtime (WinRT) APIs Cross-language infrastructure A versionof Windows
What’s the best language foryour Windows 8 apps? • The language you already know • The language your component is already written in • The language that has access to the features you need • The language that provides the right performance • The language of your choice
How do I do that? • When you write a Windows Runtime component,you can consume it in JavaScript, C#/VB, or C++
The same cross-language infrastructure that was used to create thousands of Windows Runtime APIs is available for you to write your own Windows Runtime APIs
How do I make it great? • Only where needed • Keep it simple • Async everywhere • Mind your threads
Only where needed • Indirect function calls • Type conversion
Keep it simple • Designing good Windows Runtime interfaces is similarto good OO design
Async everywhere • Use the async pattern for long running operations • The language projections make this easy
Async operation in C++ • Windows::Foundation::IAsyncOperation<uint32>^ DemoLib::GetCountAsync() • { • auto async = concurrency::create_async( • // your lambda here • [this](){ return _count;} • ); • return async; • }
Async operation in C# • public Windows.Foundation.IAsyncOperation<UInt32> GetCountAsync() • { • return (Windows.Foundation.IAsyncOperation<UInt32>)Task<UInt32>.Run( () => {return _count;} • ); • }
Mind your threads • Writing great components requires some basic knowledge of threading issues
Threading UI Thread Callasync API Create task Other work Update UI Continue Worker Thread Do work Call completion How do I get back to the main thread?
Techniques to get back to the main thread • Dispatching via the UI element • Dispatching via the core window • Proxies and stubs
Dispatching via the UI element • if (output->Dispatcher->HasThreadAccess()) • { • output->Text += "Count Changed (thread access)! Count is " + • newCount.ToString() + "\n"; • }
Dispatching via the UI element • else{ • output->Dispatcher->RunAsync( • Windows::UI::Core::CoreDispatcherPriority::Normal, • ref new Windows::UI::Core::DispatchedHandler( • [this, newCount]() -> void { • output->Text += "Count Changed (no thread access)! Count is • " + newCount.ToString() + "\n";})); • }
Dispatching via the core window • Windows::ApplicationModel::Core::CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync( • Windows::UI::Core::CoreDispatcherPriority::Normal, • ref new Windows::UI::Core::DispatchedHandler( • [this,value](){ • CountChanged(this, value);} • ));
The Windows Runtime will automatically move your calls back to the right thread if proxies and stubs are available
EventHandler vs. TypedEventHandler • event Windows::Foundation::EventHandler<uint32>^ CountChanged; • event Windows::Foundation::TypedEventHandler<IDemoLib^, uint32>^ CountChanged;
Creating a Windows Runtime in-process component sample (C++/CX) • Demo
The same cross-language infrastructure that was used to create thousands of Windows Runtime APIs is available for you to write your own Windows Runtime APIs
How do I make it great? • Only where needed • Keep it simple • Async everywhere • Mind your threads
Related Sessions • 10/30/2012 11:45 – B33 Baker – Bringing existing C++ code to Windows Store apps • 11/1/2012 12:00 – B33 Hood – Gaming reimagined: Gaming case studies A list of sessions will be sent one week prior to the event
Resources • Hybrid JavaScriptand C++ sample • Creating a Windows Runtime in-process component sample (C++/CX) Please submit session evals by using the Build Windows 8 app or at http://aka.ms/BuildSessions
Resources • Develop: http://msdn.microsoft.com/en-US/windows/apps/br229512 • Design: http://design.windows.com/ • Samples: http://code.msdn.microsoft.com/windowsapps/Windows-8-Modern-Style-App-Samples • Videos: http://channel9.msdn.com/Windows Please submit session evals by using the Build Windows 8 app or at http://aka.ms/BuildSessions