■
とりあず、async/awaiにCancelとProgressを追加。
///////////////////////////////////////////////////////////////////////////////
★C#側のWindowsランタイムコンポーネント
private async static Task
test7_3Inner(int number, System.Threading.CancellationToken canelToken, IProgress progress)
{
int rval = 0;await Task.Run( async ()=>{
int i = number;
for ( ; i < 100; i++)
{
if (canelToken.IsCancellationRequested)
{
break; // Cancel
}rval += i;
if ( i % 10 == 0 )
{
progress.Report( i ); // 0,10,20...
await Task.Delay( 1*1000 ); // wait for 1000ms.
}
}if ( i == 100 )
progress.Report(100);});
return rval; // rval == 4950 (number=0)
}
public static IAsyncOperationWithProgresstest7_3(int number)
{
// CacelのためにAsyncInfo.Runから呼ぶ。return AsyncInfo.Run(async delegate(System.Threading.CancellationToken cancelToken, IProgress
progress)
{
return await test7_3Inner(number, cancelToken, progress);
});
}//////////////////////////////////////////////////////////////////////////////////
★C++/cx側のアプリケーション (C#側のWindowsランタイムコンポーネントを呼び出す側)#include
using namespace concurrency;cancellation_token_source tk; // tk.cancel(); <-- キャンセルできる
void MainPage::test()
{
auto func = Class1::test7_3(0);
func->Progress = ref new AsyncOperationProgressHandler([](IAsyncOperationWithProgress ^ obj, int info )
{
TCHAR str[100];
swprintf_s( str,100, L"progress=%d\n", info );::OutputDebugString( str );
});create_task( func, tk.get_token() ).then( []( int sum )
{
TCHAR str[100];
swprintf_s( str,100, L"result=%d\n", sum ); // sum == 4950::OutputDebugString( str );
});
}
awaitがない、と言われたらTask.Run()で囲めばといいらしい。