none
create_task IAsyncOperation error winrt c++ metro RRS feed

  • 问题

  • 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 ?
    2012年9月19日 11:14

答案

  • 你好,

    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

    2012年9月20日 7:13
    版主

全部回复

  • 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

    2012年9月20日 6:23
    版主
  • 你好,

    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

    2012年9月20日 7:13
    版主
  • 你好, 问题已经解决。 我的 上述异步代码,含多层异步操作, 每层确实需要加入关键字 return ,同时 只有最后一个task 返回 true。

    但问题又来了, 这是最理想的 代码。 其中在任何一层加入异常处理, try catch , 或者希望跳出,终止, 编译就不通过。

    return taskA.then

    {

    // try catch.......

    // 报错,无法通过

     return taskB.then

    {

    return taskC.then

    {

    return true;

    }

    }

    }

    2012年9月26日 1:49