none
how to wait for_eachasynchronous in loop body ? RRS feed

  • 问题

  • In for_each  loop body  has two asynchronous,  how to wait  asynchronous in for_each  loop body completing , then execution out of the loop body ?

    ie:

    std::for_each(begin(files),end(files), [files]( StorageFile^ file)
    {     
           create_task(file->Properties->GetImagePropertiesAsync()).then([file](ImageProperties^ ImgProperty )                  
            { 
            //(1)Do something                    
            });       
           create_task(file->GetThumbnailAsync(ThumbnailMode::SingleItem,1024, ThumbnailOptions::UseCurrentScale)).then([file](StorageItemThumbnail^thumbnail)                               
          { 
                           
          //(2)Dosomething                                   
           });      
     }); 
     //(3)Do something 



    • 已编辑 Terry.hu 2013年6月15日 2:54
    2013年6月15日 2:51

答案

全部回复

  • 	

    我想要在所有异步都执行完做一些操作,可加上.wait()就会提示出错:中断在        __fastfail(FAST_FAIL_INVALID_ARG);

    请问怎么才能等待std::for_each中的异步全部执行完再做一些操作?

    Thanks!

    void ImagesShow::TimerShaft::GetImagePropertys(Platform::String^ folderName) {

    create_task(KnownFolders::PicturesLibrary->GetFolderAsync(folderName)).then([this](StorageFolder^ folder) {

    if (folder != nullptr) { auto getFilesTask = create_task(folder->GetFilesAsync()); getFilesTask.then( [folder](IVectorView<StorageFile^>^ files) { std::for_each(begin(files), end(files), [files]( StorageFile^ file) { if (file) { create_task(file->Properties->GetImagePropertiesAsync()).then([file]( ImageProperties^ ImgProperty ) { Windows::Foundation::DateTime dateTake; dateTake = ImgProperty->DateTaken; Array<DateTimeFormatter^> ^dateFormatters = { ref new DateTimeFormatter("month day year") }; Platform::String^ strTime = ref new String(); for (unsigned int i = 0; i < dateFormatters->Length; i++) { try { strTime = dateFormatters[i]->Format(dateTake); FormatTimeToVec(file->Name, strTime); } catch(Platform::Exception^) { dateFormatters[i]->Template + ", language: " + dateFormatters[i]->Languages->GetAt(0) + ", region: " + dateFormatters[i]->GeographicRegion + ", calendar: " + dateFormatters[i]->Calendar + ", clock:" + dateFormatters[i]->Clock + "\n"; } } Platform::String^ ImageTitle; ImageTitle = ImgProperty->Title; g_MapImageTitle.insert(pair<Platform::String^,Platform::String^>(file->Name,file->Name));//ImgProperty->Title; });//.wait(); create_task(file->GetThumbnailAsync(ThumbnailMode::SingleItem, 1024, ThumbnailOptions::UseCurrentScale)).then([file](StorageItemThumbnail^ thumbnail) { if (thumbnail != nullptr) { auto bitmapImage = ref new BitmapImage(); bitmapImage->SetSource(thumbnail); g_MapImageSource.insert(pair<Platform::String^,Windows::UI::Xaml::Media::ImageSource^>(file->Name,bitmapImage)); g_MapImageFile.insert(pair<Platform::String^,StorageFile^>(file->Name,file)); } });//.wait(); } }); }); } }); }

    加上.wait()就出现错误,中断就出现error指向:TaskCollection.cpp中的            __fastfail(FAST_FAIL_INVALID_ARG);

    调用堆栈:

    > msvcr110d.dll!Concurrency::details::_ReportUnobservedException() 行 2455 C++
      ImagesShow.exe!Concurrency::details::_ExceptionHolder::~_ExceptionHolder() 行 882 C++
      ImagesShow.exe!Concurrency::details::_ExceptionHolder::`scalar deleting destructor'(unsigned int) C++
      ImagesShow.exe!std::_Ref_count_obj<Concurrency::details::_ExceptionHolder>::_Destroy() 行 885 C++
      ImagesShow.exe!std::_Ref_count_base::_Decref() 行 120 C++
      ImagesShow.exe!std::_Ptr_base<Concurrency::details::_ExceptionHolder>::_Decref() 行 347 C++
      ImagesShow.exe!std::shared_ptr<Concurrency::details::_ExceptionHolder>::~shared_ptr<Concurrency::details::_ExceptionHolder>() 行 624 C++
      ImagesShow.exe!Concurrency::details::_Task_impl_base::~_Task_impl_base() 行 1294 C++
      ImagesShow.exe!Concurrency::details::_Task_impl<unsigned char>::~_Task_impl<unsigned char>() 行 1972 C++
      ImagesShow.exe!Concurrency::details::_Task_impl<unsigned char>::`scalar deleting destructor'(unsigned int) C++
      ImagesShow.exe!std::_Ref_count_obj<Concurrency::details::_Task_impl<unsigned char> >::_Destroy() 行 884 C++
      ImagesShow.exe!std::_Ref_count_base::_Decref() 行 120 C++
      ImagesShow.exe!std::_Ptr_base<Concurrency::details::_Task_impl_base>::_Decref() 行 347 C++
      ImagesShow.exe!std::shared_ptr<Concurrency::details::_Task_impl_base>::~shared_ptr<Concurrency::details::_Task_impl_base>() 行 624 C++
      ImagesShow.exe!Concurrency::details::_Task_impl_base::_RunContinuation(Concurrency::details::_ContinuationTaskHandleBase * _PTaskHandle=0x028cced4) 行 1627 C++
      ImagesShow.exe!Concurrency::details::_Task_impl_base::_RunTaskContinuations() 行 1781 C++
      ImagesShow.exe!Concurrency::details::_Task_impl<Windows::Foundation::Collections::IVectorView<Windows::Storage::StorageFile ^> ^>::_FinalizeAndRunContinuations(Windows::Foundation::Collections::IVectorView<Windows::Storage::StorageFile ^> ^ _Result=0x06f44568) 行 2088 C++
      ImagesShow.exe!<lambda_be2b00aae094934c17e42bb5d3b6e88b>::operator()(Windows::Foundation::IAsyncOperation<Windows::Foundation::Collections::IVectorView<Windows::Storage::StorageFile ^> ^> ^ _Operation=0x06f159c8, Windows::Foundation::AsyncStatus _Status=Completed) 行 1856 C++
      ImagesShow.exe!Platform::Details::__abi_FunctorCapture2<<lambda_be2b00aae094934c17e42bb5d3b6e88b>,void,Windows::Foundation::IAsyncOperation<Windows::Foundation::Collections::IVectorView<Windows::Storage::StorageFile ^> ^> ^,enum Windows::Foundation::AsyncStatus>::Invoke(Windows::Foundation::IAsyncOperation<Windows::Foundation::Collections::IVectorView<Windows::Storage::StorageFile ^> ^> ^ __arg0=0x06f159c8, Windows::Foundation::AsyncStatus __arg1=Completed) 行 864 C++
      ImagesShow.exe!Windows::Foundation::AsyncOperationCompletedHandler<Windows::Foundation::Collections::IVectorView<Windows::Storage::StorageFile ^> ^>::Invoke(Windows::Foundation::IAsyncOperation<Windows::Foundation::Collections::IVectorView<Windows::Storage::StorageFile ^> ^> ^ __param0=0x06f159c8, Windows::Foundation::AsyncStatus __param1=Completed) C++
    __param0=0x06f159c8, Windows::Foundation::AsyncStatus __param1=Completed) C++
      shell32.dll!75e0d9a9() 未知
      [下面的框架可能不正确和/或缺失,没有为 shell32.dll 加载符号] 
      shell32.dll!75dfb211() 未知
      shell32.dll!75d9fee7() 未知
      shell32.dll!75d9f622() 未知
      kernel32.dll!776e1866() 未知
      ntdll.dll!77db6911() 未知
      ntdll.dll!77db68bd() 未知

    求高手!解决怎么等待for_each里面的异步执行完之后for_each之外的再执行???


    2013年6月9日 11:05
  • void ImagesShow::TimerShaft::GetImagePropertys(Platform::String^ folderName)
    {	   		
      create_task(KnownFolders::PicturesLibrary->GetFolderAsync(folderName)).then([this](StorageFolder^ folder)
     {
        if (folder != nullptr)
        {			
    	create_task(folder->GetFilesAsync()).then( [folder](IVectorView<StorageFile^>^ files) 
    	{
    	  std::for_each(begin(files), end(files), [files](  StorageFile^ file)
              {					
    	   if (file)
    	       {    
    
                        create_task(file->Properties->GetImagePropertiesAsync()).then([file]( ImageProperties^ ImgProperty  )		
                        {							
                            //(1)Do something  								
                        });						 
                         create_task(file->GetThumbnailAsync(ThumbnailMode::SingleItem, 1024, ThumbnailOptions::UseCurrentScale)).then([file](StorageItemThumbnail^ thumbnail)				             
    
                          {							
                            //(2)Do something  		              		
                          });					 
                    }
               });						
            }); 			
          }
          //(3)Do something 
       });	
    }
    
    
    Hi, excuse me how wait for (1) (2) perform the execution (3) again??
    2013年6月14日 1:50
  • void ImagesShow::TimerShaft::GetImagePropertys(Platform::String^ folderName)
    {	   		
      create_task(KnownFolders::PicturesLibrary->GetFolderAsync(folderName)).then([this](StorageFolder^ folder)
     {
        if (folder != nullptr)
        {			
    	create_task(folder->GetFilesAsync()).then( [folder](IVectorView<StorageFile^>^ files) 
    	{
    	  std::for_each(begin(files), end(files), [files](  StorageFile^ file)
              {					
    	   if (file)
    	       {    
    
                        create_task(file->Properties->GetImagePropertiesAsync()).then([file]( ImageProperties^ ImgProperty  )		
                        {							
                            //(1)Do something  								
                        });						 
                         create_task(file->GetThumbnailAsync(ThumbnailMode::SingleItem, 1024, ThumbnailOptions::UseCurrentScale)).then([file](StorageItemThumbnail^ thumbnail)				             
    
                          {							
                            //(2)Do something  		              		
                          });					 
                    }
               });						
            }); 			
          }
          //(3)Do something 
       });	
    }
    


       Hi, excuse me how wait for (1) (2) perform finished, then execution (3) again??
    2013年6月14日 1:57
  • help!help!

    2013年6月14日 3:31
  • 你可以尝试把循环里创建的两个task,接收其返回值 然后调用 task.get() (将等待任务完成)

    这样循环里面相当于是同步执行。

    然后继续 .then

    2013年6月14日 8:45
  • 你可以尝试把循环里创建的两个task,接收其返回值 然后调用 task.get() (将等待任务完成)

    这样循环里面相当于是同步执行。

    然后继续 .then

    怎么接受返回值?
    2013年6月14日 9:24
  • help!help!
    2013年6月14日 9:32
  • help
    2013年6月17日 1:18
  • Hi there,

    Using create_task.then , check this blog: http://blogs.msdn.com/b/pfxteam/archive/2010/11/21/10094564.aspx and task<InMemoryRandomAccessStream^> ThumbnailGenerator::CreateThumbnailFromPictureFileAsync(
        StorageFile^ sourceFile,  unsigned int thumbSize) function implementation on MSDN doc: http://msdn.microsoft.com/en-us/library/windows/apps/jj160321.aspx

    Regards,

    Jenny

     

    2013年7月15日 9:09