Kinect in Motion – Audio and Visual Tracking by Example

Together with Clemente Giorio we just released our first book on Kinect for Windows SD. Happy to hear what you think
Step-by-step examples on how to master the essential features of Kinect technology
Fully-functioning code samples ready to expand and adjust to your need
Compact and handy reference on how to adopt a multimodal user interface in your application
http://www.packtpub.com/kinect-motion-for-audio-and-visual-tracking/book

Kinect in Motion – Audio and Visual Tracking by Example

Advertisements

How to handle a tight loop on the UI Thread in .Net 4.5 and .Net 4.0

Recently I developed a PoC on Kinect for Windows v 1.6 where I had to retrieve Color Frames using the polling technique in C# (.Net 4.5 and .Net 4.0).

I had some fun (especially on .Net 4.0) on managing the tight loop that keeps the UI responsive to the user input. Here is my lesson learned, it may can save some of your time if you want to do the same in any type of WPF or WinRT or Windows Phone 8 App (regardless you are using the Kinect Sensor).

For simplicity I will deal with the following scenario (you can adapt the same to yours):

we have a textbox that you want to update continuously till we pause the update using a pause/resume button.

The following XAML defines the UI controls (optionally we can use a Grid to handle them)

<Grid>

<Grid.RowDefinitions>

<RowDefinition Height=”Auto” />

<RowDefinition Height=”*” />

</Grid.RowDefinitions>

<Button Grid.Row=”0″ FontSize=”30″ x:Name=”btnTest” Content=”Start” Click=”btnTest_Click” />

<TextBox Grid.Row=”1″ FontSize=”30″ x:Name=”txtOutput” />

</Grid>

let’s consider a fictitious work we want to perform and the related update of the UI

private int testValue;

private void DoOurWork() {

Thread.Sleep(100);

Random rnd = new Random();

testValue = rnd.Next();

}

bool isRunning;

private void DoTheUpdate()  {

if (isRunning) {

this.txtOutput.Text = string.Format(“Test box updated {0}”, testValue); }

}

We manage the button click event in order to pause/resume the textbox update

private void btnTest_Click(object sender, RoutedEventArgs e) {

if (isRunning) {

btnTest.Content = “Start”;}

else  {

btnTest.Content = “Stop”;}

isRunning = !isRunning;         }

we may are tempted to perform a simple tight loop in the MainWindow_Loaded (the event handler of the MainWindow Loaded event) using

void MainWindow_Loaded(object sender, RoutedEventArgs e) {

while (true) {

DoOurWork();

DoTheUpdate(); }

}

but this is not able to handle the user interaction, the WPF application will not respond.

In .Net 4.5 we can solve the issue easily using the await async keywords. We can update the code in the following way:

first of all we transform the DoOurWork method in an asynchronous one

private async Task DoOurWorkAsync() {

await Task.Run(() => {

Thread.Sleep(100);

Random rnd = new Random();

testValue = rnd.Next();

});

}

secondly we refactor the MainWindow_Loaded using the await technique

async void MainWindow_Loaded(object sender, RoutedEventArgs e) { while (true)

{  await DoOurWorkAsync();

DoTheUpdate(); }

}

end we are good to go!

be sure to NOT include the update of the UI in an async method. this will cause the Exception System.InvalidOperationException “The calling thread cannot access this object because a different thread owns it” as async method and running in a different thread then the UI one.

In .Net 4.0 things are less immediate and we need to do more rework.

first of all we need to define an instance of the BackgroundWorker class that will manage the async peration and update the UI only when the async operation is completed.

secondly we have to refactor the MainWindow_Loaded event handler as per:

void MainWindow_Loaded(object sender, RoutedEventArgs e)   {

this.backgroundWorker1.RunWorkerCompleted += backgroundWorker1_RunWorkerCompleted;

this.backgroundWorker1.DoWork += backgroundWorker1_DoWork;

if (!this.backgroundWorker1.IsBusy)

this.backgroundWorker1.RunWorkerAsync();

}

the DoWork event is raised anytime we request to execute the RunWorkerAsync() method

the RunWorkerCompleted event is raised as soon as the execution of the RunWorkerAsync() method is completed.

The backgroundWorker1_DoWork event handler takes care to execute our work

void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e){

DoOurWork(); }

The backgroundWorker1_RunWorkerCompleted event handler updates the UI as it is running on the same thread and a submit a new request to perform our work in order to handle the “infinite” loop.

private void backgroundWorker1_RunWorkerCompleted(object sender,             RunWorkerCompletedEventArgs e) {

DoTheUpdate();

//restart the manipulation

this.backgroundWorker1.RunWorkerAsync();

}

System.ArgumentException “Buffer size is not sufficient.”

How to avoid the System.ArgumentException “Buffer size is not sufficient.”

Getting and Displaying Color Data in C# Using the Raw Bayer Format – How to avoid the System.ArgumentException “Buffer size is not sufficient.”

please read my post to the Kinect for Windows SDK documentation

http://msdn.microsoft.com/en-us/library/jj663792.aspx

WinRT does use Win32

While getting my head around on how and if WinRT is related to Win32 I came across several “official” and “not official” documentation picturing WinRT as it resides directly on top of the kernel just like the Win32 API which I read “WinRT and Win32 are independent from each other”.

Reading and researching with more attention I came across “An Accurate Windows 8 Platform Architecture Diagram?” blog that does challenge the “WinRT and Win32 are independent from each other” statement.

To prove the same to myself I just jumped on Visual Studio, open few Windows 8 app samples (http://code.msdn.microsoft.com/windowsapps/Windows-8-Modern-Style-App-Samples) in C++ put a breakpoint in the code and had a look at what is coming out in the Call Stack window. From there you can see all the call done to the Win32 APIs (e.g. Kernel32.dll, User32.dll, etc.) and proof to myself that yes WinRT does use Win32.

The picture below is a view on the Stack Call once we call the Windows.Media.Capture.CameraCaptureUI WinRT API and you can notice the call to the User32.dll Win32 API.

WinRT and Win32 - 1

the following stack is another example on the output you can obtain  when calling the Windows::Security::Credentials::UI::CredentialPickerResults

CredentialPickerCPP.exe!<lambda_e8a44fd0935f1cd6d5a97e5fd3c74bce>::operator()(Windows::Security::Credentials::UI::CredentialPickerResults ^ credPickerResult) Line 116 C++
CredentialPickerCPP.exe!std::_Callable_obj<<lambda_e8a44fd0935f1cd6d5a97e5fd3c74bce>,0>::

_ApplyX<void,Windows::Security::Credentials::UI::CredentialPickerResults ^>(Windows::Security::Credentials::UI::CredentialPickerResults ^ && _V0) Line 431 C++
CredentialPickerCPP.exe!std::_Func_impl<std::_Callable_obj<<lambda_e8a44fd0935f1cd6d5a97e5fd3c74bce>,0>

,std::allocator<std::_Func_class<void,Windows::Security::Credentials::UI::CredentialPickerResults ^,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil> >,void,Windows::Security::Credentials::UI::CredentialPickerResults ^,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil>::_Do_call(Windows::Security::Credentials::UI::CredentialPickerResults ^ && _V0) Line 239 C++
CredentialPickerCPP.exe!std::_Func_class<void,Windows::Security::Credentials::UI::CredentialPickerResults ^,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil>::operator()(Windows::Security::Credentials::UI::CredentialPickerResults ^ _Vx0) Line 515 C++
CredentialPickerCPP.exe!<lambda_3eff6d67b594a157667e680fe7b183b5>::operator()(Windows::Security::Credentials::UI::CredentialPickerResults ^ t) Line 2185 C++
CredentialPickerCPP.exe!std::_Callable_obj<<lambda_3eff6d67b594a157667e680fe7b183b5>,0>::_ApplyX<unsigned char,Windows::Security::Credentials::UI::CredentialPickerResults ^>(Windows::Security::Credentials::UI::CredentialPickerResults ^ && _V0) Line 431 C++
CredentialPickerCPP.exe!std::_Func_impl<std::_Callable_obj<<lambda_3eff6d67b594a157667e680fe7b183b5>,0>

,std::allocator<std::_Func_class<unsigned char,Windows::Security::Credentials::UI::CredentialPickerResults ^,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil> >,unsigned char,Windows::Security::Credentials::UI::CredentialPickerResults ^,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil>::_Do_call(Windows::Security::Credentials::UI::CredentialPickerResults ^ && _V0) Line 239 C++
CredentialPickerCPP.exe!std::_Func_class<unsigned char,Windows::Security::Credentials::UI::CredentialPickerResults ^,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil>::operator()(Windows::Security::Credentials::UI::CredentialPickerResults ^ _Vx0) Line 515 C++
CredentialPickerCPP.exe!Concurrency::task<Windows::Security::Credentials::UI::CredentialPickerResults ^>::_ContinuationTaskHandle<Windows::Security::Credentials::UI::CredentialPickerResults ^,void,<lambda_e8a44fd0935f1cd6d5a97e5fd3c74bce>,std::integral_constant<bool,0>,

Concurrency::details::_TypeSelectorNoAsync>::_Continue(std::integral_constant<bool,0> __formal, Concurrency::details::_TypeSelectorNoAsync __formal) Line 3327 C++
CredentialPickerCPP.exe!Concurrency::task<Windows::Security::Credentials::UI::CredentialPickerResults ^>::_ContinuationTaskHandle<Windows::Security::Credentials::UI::CredentialPickerResults ^,void,<lambda_e8a44fd0935f1cd6d5a97e5fd3c74bce>,std::integral_constant<bool,0>,

Concurrency::details::_TypeSelectorNoAsync>::_Perform() Line 3318 C++
CredentialPickerCPP.exe!Concurrency::details::_PPLTaskHandle<unsigned char,Concurrency::task<Windows::Security::Credentials::UI::CredentialPickerResults ^>::_ContinuationTaskHandle<Windows::Security::Credentials::UI::CredentialPickerResults ^,void,<lambda_e8a44fd0935f1cd6d5a97e5fd3c74bce>,std::integral_constant<bool,0>,

Concurrency::details::_TypeSelectorNoAsync>,Concurrency::details::_ContinuationTaskHandleBase>::operator()() Line 1213 C++
CredentialPickerCPP.exe!Concurrency::details::_UnrealizedChore::_InvokeBridge<Concurrency::details::_PPLTaskHandle<unsigned char,Concurrency::task<Windows::Security::Credentials::UI::CredentialPickerResults ^>::_ContinuationTaskHandle<Windows::Security::Credentials::UI::CredentialPickerResults ^,void,<lambda_e8a44fd0935f1cd6d5a97e5fd3c74bce>,std::integral_constant<bool,0>,

Concurrency::details::_TypeSelectorNoAsync>,Concurrency::details::_ContinuationTaskHandleBase> >(Concurrency::details::_PPLTaskHandle<unsigned char,Concurrency::task<Windows::Security::Credentials::UI::CredentialPickerResults ^>::_ContinuationTaskHandle<Windows::Security::Credentials::UI::CredentialPickerResults ^,void,<lambda_e8a44fd0935f1cd6d5a97e5fd3c74bce>,std::integral_constant<bool,0>,

Concurrency::details::_TypeSelectorNoAsync>,Concurrency::details::_ContinuationTaskHandleBase> * _PChore) Line 4467 C++
msvcr110d.dll!Concurrency::details::_TaskCollection::_RunAndWait(Concurrency::details::_UnrealizedChore * pChore) Line 1607 C++
CredentialPickerCPP.exe!Concurrency::details::_AsyncTaskCollection::_ScheduleWithAutoInline(Concurrency::details::_UnrealizedChore * _PChore, Concurrency::details::_TaskInliningMode _InliningMode) Line 5472 C++
CredentialPickerCPP.exe!Concurrency::details::_Task_impl_base::_ScheduleTask(Concurrency::details::_UnrealizedChore * _PTaskHandle, Concurrency::details::_TaskInliningMode _InliningMode) Line 1564 C++
CredentialPickerCPP.exe!<lambda_1f5511c73163eb4d5fb62ada220cb13c>::operator()() Line 1671 C++
CredentialPickerCPP.exe!std::_Callable_obj<<lambda_1f5511c73163eb4d5fb62ada220cb13c>,0>::_ApplyX<void>() Line 431 C++
CredentialPickerCPP.exe!std::_Func_impl<std::_Callable_obj<<lambda_1f5511c73163eb4d5fb62ada220cb13c>,0>,

std::allocator<std::_Func_class<void,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil> >,void,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil>::_Do_call() Line 239 C++
CredentialPickerCPP.exe!std::_Func_class<void,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil>::operator()() Line 514 C++
CredentialPickerCPP.exe!Concurrency::details::_ContextCallback::_Bridge(tagComCallData * _PParam) Line 572 C++
combase.dll!74f4178f() Unknown
[Frames below may be incorrect and/or missing, no symbols loaded for combase.dll]
combase.dll!74f7f00c() Unknown
rpcrt4.dll!768ee980() Unknown
rpcrt4.dll!7696ade1() Unknown
Windows.UI.Xaml.dll!02cd8c8b() Unknown
Windows.UI.Xaml.dll!02e18c9d() Unknown
Windows.UI.Xaml.dll!02cd7af0() Unknown
Windows.UI.Xaml.dll!02e1857f() Unknown
Windows.UI.Xaml.dll!02e152a0() Unknown
Windows.UI.Xaml.dll!02edb82a() Unknown
Windows.UI.Xaml.dll!02f714a0() Unknown
Windows.UI.Xaml.dll!02f73ca5() Unknown
Windows.UI.Xaml.dll!02e190ca() Unknown
combase.dll!74fbb1cf() Unknown
combase.dll!74fbb0d5() Unknown
combase.dll!74ed030b() Unknown
uxtheme.dll!6f06249f() Unknown
ntdll.dll!76f51318() Unknown
KernelBase.dll!745cc752() Unknown
combase.dll!74f3189c() Unknown
combase.dll!74fc23a8() Unknown
  user32.dll!766f76f5() Unknown
user32.dll!766f8fba() Unknown
user32.dll!766f76f5() Unknown
user32.dll!766f9879() Unknown
user32.dll!766f7933() Unknown
user32.dll!766f787a() Unknown
user32.dll!766f7933() Unknown
user32.dll!766f899d() Unknown
user32.dll!766f7a8a() Unknown
user32.dll!766f7aa2() Unknown
Windows.UI.dll!0f9a119c() Unknown
Windows.UI.dll!0f9a1256() Unknown
Windows.UI.Xaml.dll!02e2d84a() Unknown
Windows.UI.Xaml.dll!02e2d809() Unknown
Windows.UI.Xaml.dll!02e2d7cd() Unknown
ntdll.dll!76f601a0() Unknown
twinapi.dll!561cc9dd() Unknown
twinapi.dll!561ccab2() Unknown
twinapi.dll!561cc9f6() Unknown
SHCore.dll!71cc5a10() Unknown
kernel32.dll !@BaseThreadInitThunk@12() Unknown
ntdll.dll!76f6ac69() Unknown
ntdll.dll!76f6ac3c() Unknown

Simple and effective way to convert int to string (and back) in C++/CX XAML Windows Store App

This post provides you a simple way to convert int to string and back in c++/cx XAML (using textbox)

your .xaml file

<StackPanel>
<TextBlock Name=”tbNumberOne”>First Number</TextBlock>
<TextBox x:Name=”inputOne”></TextBox>
<TextBlock Name=”tbNumberTwo”>Second Number</TextBlock>
<TextBox x:Name=”inputTwo”></TextBox>
<Button Content=”Add” Name=”btnAdd” Click=”btnAdd_Click_1″></Button>
<TextBlock Name=”tbResult”></TextBlock>
</StackPanel>

the .cs file

 

void App1Cpp::MainPage::btnAdd_Click_1(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)

{

int firstnumber = _wtoi(inputOne->Text->Data());

int secondnumber =_wtoi(inputTwo->Text->Data());  int result = firstnumber + secondnumber;

tbResult->Text = result.ToString();

}

The Windows Phone Emulator wasn’t able to create the virtual machine

If you incur in the error “The Windows Phone Emulator wasn’t able to create the virtual machine something happened while creating a switch: Xde couldn’t find an IPv4 address for the host machine.” (please refer the figure n.2) you should ensure that your Hyper-V Manager includes the configured  switch named “Windows Phone Emulator Internal Switch” set as in figure n.1

Figure n.1

Figure n.2

Windows 8 Compatibility Center

Windows 8 Compatibility Center http://www.microsoft.com/en-gb/windows/compatibility/win8/CompatCenter/Home provides a powerful tool for what works (Apps and devices) on Windows 8. Around 88% of the applications (desktop mode) are running on Windows 8 (pro) with only 7% not compatible applications.

The most affected categories are PC gaming and Security.

Microsoft itself is paying the cost of the upgrade (at least 14% of the not compatible applications are produced by Microsoft)

Some notorious applications are not supported Visual Studio 2010 Premium, Norton Internet Security 2012, McAfee total protection version 11, Office 2003 Professional, Zune ver 4, Windows Phone 7 SDK, SAP GUI ver 7, etc.

 


Please note that figures have been computed as my personal PoV on filters offered by the  Compatibility Center. None of the figures and charts published in this post is meant to be Microsoft official data.