积极答复者
create_task IAsyncOperation error winrt c++ metro

问题
-
the same code for two functions , but the TestASync worked error [error reports:Unhandled exception at 0x5FA42BF8 (msvcr110.dll) in CPPApp_testwinrtcomponent.exe: An invalid parameter was passed to a function that considers invalid parameters fatal.] ,yet
the function [ bool test] worked right.
IAsyncOperation<bool>^ ClassTest::TestASync(Platform::String^ aFpath) { return Concurrency::create_async([aFpath]() ->Boolean { Platform::String^ Fpath=aFpath; create_task(StorageFile::GetFileFromPathAsync(Fpath)).then([](task<StorageFile^> taskgetfile) { try { StorageFile^ file=taskgetfile.get(); if(file==nullptr) return false; create_task(file->OpenAsync(FileAccessMode::ReadWrite)).then([](IRandomAccessStream^ rastream) { IOutputStream^ outputStream =rastream->GetOutputStreamAt(0); DataWriter^ dwrite = ref new DataWriter(outputStream); dwrite->UnicodeEncoding=UnicodeEncoding::Utf8; int Fill[4]; for(int i=0;i<4;i++) Fill[i]=rand(); Array<unsigned char,1>^ inarray =ref new Array<unsigned char,1>(16); memcpy(inarray->Data,Fill,16); dwrite->WriteBytes(inarray); create_task(dwrite->StoreAsync()).then([outputStream](task<unsigned int> taskoutstream) { try { outputStream->FlushAsync(); // return ; } catch(...) { return ; } }); }); } catch(...) { return false; } }); return true; }); } bool ClassTest::test(Platform::String^ aFpath) { Platform::String^ Fpath=aFpath; create_task(StorageFile::GetFileFromPathAsync(Fpath)).then([](task<StorageFile^> taskgetfile) { try { StorageFile^ file=taskgetfile.get(); if(file==nullptr) return false; create_task(file->OpenAsync(FileAccessMode::ReadWrite)).then([](IRandomAccessStream^ rastream) { IOutputStream^ outputStream =rastream->GetOutputStreamAt(0); DataWriter^ dwrite = ref new DataWriter(outputStream); dwrite->UnicodeEncoding=UnicodeEncoding::Utf8; int Fill[4]; for(int i=0;i<4;i++) Fill[i]=rand(); Array<unsigned char,1>^ inarray =ref new Array<unsigned char,1>(16); memcpy(inarray->Data,Fill,16); dwrite->WriteBytes(inarray); create_task(dwrite->StoreAsync()).then([outputStream](task<unsigned int> taskoutstream) { try { outputStream->FlushAsync(); // return ; } catch(...) { return ; } }); }); } catch(...) { return false; } }); return true; }
what's wrong ?
答案
-
你好,
Windows runtime 需要返回Windows runtime的数据类型。
Boolean 是Windows runtime的数据类型, bool是C++的基本类型。
如果你的函数不是private的话,返回值必须是Windows runtime的数据类型。
http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh700121.aspx
请参考这段代码。
// In .h file Windows::Foundation::IAsyncOperation<Platform::Boolean>^ TestASync(Platform::String^ aFpath); // In cpp file IAsyncOperation<Platform::Boolean>^ MainPage::TestASync(Platform::String^ aFpath) { return Concurrency::create_async([aFpath]() ->Platform::Boolean{ Platform::String^ Fpath=aFpath; return true;}); }
Jesse Jiang [MSFT]
MSDN Community Support | Feedback to us
- 已标记为答案 g2008apple163.com 2012年9月26日 1:45
全部回复
-
The bool Test will retuen true immediately while the task is running. It may not return the correct value in your create_task lambda. So that works fine.
And how do you call your async method? Did you do create_task to invoke the TestASync?
Bob Bao [MSFT]
MSDN Community Support | Feedback to us
-
你好,
Windows runtime 需要返回Windows runtime的数据类型。
Boolean 是Windows runtime的数据类型, bool是C++的基本类型。
如果你的函数不是private的话,返回值必须是Windows runtime的数据类型。
http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh700121.aspx
请参考这段代码。
// In .h file Windows::Foundation::IAsyncOperation<Platform::Boolean>^ TestASync(Platform::String^ aFpath); // In cpp file IAsyncOperation<Platform::Boolean>^ MainPage::TestASync(Platform::String^ aFpath) { return Concurrency::create_async([aFpath]() ->Platform::Boolean{ Platform::String^ Fpath=aFpath; return true;}); }
Jesse Jiang [MSFT]
MSDN Community Support | Feedback to us
- 已标记为答案 g2008apple163.com 2012年9月26日 1:45