locked
A task question

    Question

  • I have a simple async task function GetFileType() that simply gets file from the Pictures folder and returns the type of the file. This is a simple example and the intention behind is to learn how to write our own asyn functions that return a value. But I am getting an access violation error. What could be wrong? I would also like to know if this is the expected way async functions that return value should be written( especially , can we avoid multiple return statements)
    void TestUI::MainPage::Button_Click_6(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
    {
    
    task<String^>( GetFileType()).then([this](task<String^>v)
    {
    try
    {
    	String^ ret = dynamic_cast<String^>(v.get());
    }
    catch( Exception^ e )
    {
    
    }
    });
    }

    IAsyncOperation<String^>^ TestUI::MainPage::GetFileType( ) { String^ val; return create_async([this,&val]()->String^ { task<Windows::Storage::StorageFile^>(Windows::Storage::KnownFolders::PicturesLibrary->GetFileAsync( "Peacock.jpg" )). then([&val](task<Windows::Storage::StorageFile^>gettask )->String^ { try { StorageFile^ file = dynamic_cast<StorageFile^>(gettask.get()); val = file->FileType; return val; } catch( Exception^ e ) { } }); return val; }); }

    The exception is below:


    • Edited by its_me_here Monday, January 14, 2013 1:20 PM
    Monday, January 14, 2013 1:17 PM

Answers

  • Hi,

    I tested your codes and found that you return the val twice.

    The last return val, actually it does not run the task before return. And the value should be null function should be return, but it binding into the next lambda express, so the lambda return the second time, it throw error. Because the val return twice.

    I changed the codes. Please follow this

    IAsyncOperation<String^>^ App1::MainPage::GetFileType( )
    {
    	
    	return create_async([this]()->String^
    	{
    		
    		auto getfile= task<Windows::Storage::StorageFile^>(Windows::Storage::KnownFolders::PicturesLibrary->GetFileAsync( "3.jpg" )).
    			then([](task<Windows::Storage::StorageFile^>gettask )->String^
    		{
    			try
    			{
    				StorageFile^ file = dynamic_cast<StorageFile^>(gettask.get());
    				String^ val2;
    				val2 = file->FileType;
    				
    				return val2;
    				
    			}
    			catch( Exception^ e )
    			{
    			}
    		});
    		return getfile.get();
    	});
    }

     


    Best regards,
    Jesse


    Jesse Jiang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by its_me_here Tuesday, January 15, 2013 11:58 AM
    Tuesday, January 15, 2013 7:31 AM

All replies

  • Have yo uset the application manifest to allow reading from pictureslibrary ?


    n.Wright

    Monday, January 14, 2013 3:23 PM
  • Hi , yes.I have set the Pictures Library permission in manifest..

    Monday, January 14, 2013 6:38 PM
  • Here is my load file routine.

    void SimpleTextRenderer::loadfile()
    {
    	
        //pick pcb      
    	FileOpenPicker^ openPicker = ref new FileOpenPicker();
    	openPicker->ViewMode = PickerViewMode::List;   //or Thumbnail;
    	openPicker->SuggestedStartLocation = PickerLocationId::PicturesLibrary;
        openPicker->FileTypeFilter->Append(".pcb");
    
    	
    		task<StorageFile^> fileTask(openPicker->PickSingleFileAsync());
    	fileTask.then([=](StorageFile^ file) {
    
    		if (file==nullptr)
    			return;
    
    
    
    		String^ fileName="4000.pcb"; 
    
    
    		fileName=file->Name;
    		previousfname=fileName;
    		gerberfilename=file->DisplayName; //get name with no extension !!!
    	
    	StorageFolder^ item = KnownFolders::PicturesLibrary;
     
      auto getFileOp = item->GetFileAsync(fileName);
      getFileOp->Completed = ref new AsyncOperationCompletedHandler<StorageFile^>
        ([=](IAsyncOperation<StorageFile^>^ operation, AsyncStatus status)
      {
        auto storageFile = operation->GetResults();
        auto openOp = storageFile->OpenAsync(FileAccessMode::Read);
        openOp->Completed = 
          ref new AsyncOperationCompletedHandler <IRandomAccessStream^>
          ([=](IAsyncOperation<IRandomAccessStream^>^ operation, AsyncStatus status)
        {
          auto istream = operation->GetResults();
          auto dr = ref new DataReader(istream);
          auto loadOp = dr->LoadAsync(istream->Size);
          loadOp->Completed = ref new AsyncOperationCompletedHandler<UINT>
            ([=](IAsyncOperation<UINT>^ operation, AsyncStatus status)
          {
            auto bytesRead = operation->GetResults();
    
    
    		int cx,wurd,dx;	
    		wurd=0;
               //wurd=(UINT16)rev(dr->ReadUInt16());
    
    
    		
                ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
                //{load SYMBOLS}
    
                wurd=(UINT16)rev(dr->ReadUInt16());
                cx = wurd;
                wurd=(UINT16)rev(dr->ReadUInt16());
                cx = cx + (wurd * 65536);
    
                for (dx = 1; dx <= cx; dx++)
                {
                    wurd=(UINT16)rev(dr->ReadUInt16());
                    symboldata[dx] = wurd;
                }
    
                ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
                //{load TRACKS}
                wurd=(UINT16)rev(dr->ReadUInt16());
                cx = wurd;
                wurd=(UINT16)rev(dr->ReadUInt16());
                cx = cx + (wurd * 65536);
    
                for (dx = 1; dx <= cx; dx++)
                {
                    wurd=(UINT16)rev(dr->ReadUInt16());
                    trackdata[dx] = wurd;
                }
    
    
                ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
                //{load PADS}
                wurd=(UINT16)rev(dr->ReadUInt16());
                cx = wurd;
                wurd=(UINT16)rev(dr->ReadUInt16());
                cx = cx + (wurd * 65536);
    
                for (dx = 1; dx <= cx; dx++)
                {
                    wurd=(UINT16)rev(dr->ReadUInt16());
                    //	{padata[cx]=wurd;}
                }
    
    
                ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
                //{load TEXT}
                wurd=(UINT16)rev(dr->ReadUInt16());
                cx = wurd;
                wurd=(UINT16)rev(dr->ReadUInt16());
                cx = cx + (wurd * 65536);
    
                for (dx = 1; dx <= cx; dx++)
                {
                    wurd=(UINT16)rev(dr->ReadUInt16());
                    textdata[dx] = wurd;
                }
    
    
    
                ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
                //{load CONS}
                wurd=(UINT16)rev(dr->ReadUInt16());
                cx = wurd;
                wurd=(UINT16)rev(dr->ReadUInt16());
                cx = cx + (wurd * 65536);
    
                for (dx = 1; dx <= cx; dx++)
                {
                    wurd=(UINT16)rev(dr->ReadUInt16());
                    //	{condata[dx]=wurd;}
                }
    
    
    
    
    
                ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
                //{load INTLIB}
    
    
                wurd=(UINT16)rev(dr->ReadUInt16());
                cx = wurd;
                wurd=(UINT16)rev(dr->ReadUInt16());
                cx = cx + (wurd * 65536);
    
                for (dx = 1; dx <= cx; dx++)
                {
                    wurd=(UINT16)rev(dr->ReadUInt16());
                    slibrary[dx] = wurd;
                }
    
    			dr->DetachStream();
    
    
                ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
    
                removeduplicatessymbols(); //in case some schems got duplicated
    
                lineuptracks();// {in case pcb come from translator with new symbol shapes}
    
    
    
    //			loadrefstring();
    				adjustzoom();
    				redraw();
    
    			
    			/* print out load file size
    				//
                char buffer [33];
    			_itoa_s(bytesRead, buffer,10);
    				titlestring=buffer;
    				shortredraw();
    				*/
    
    
            
          });
        });
      });
    
      });
    	
    			  
    }
    


    n.Wright

    Monday, January 14, 2013 9:19 PM
  • Thank you wrightylplp for your reply. But my question is about coding our own async function that returns a value and calling that function.
    Tuesday, January 15, 2013 5:53 AM
  • Hi,

    I tested your codes and found that you return the val twice.

    The last return val, actually it does not run the task before return. And the value should be null function should be return, but it binding into the next lambda express, so the lambda return the second time, it throw error. Because the val return twice.

    I changed the codes. Please follow this

    IAsyncOperation<String^>^ App1::MainPage::GetFileType( )
    {
    	
    	return create_async([this]()->String^
    	{
    		
    		auto getfile= task<Windows::Storage::StorageFile^>(Windows::Storage::KnownFolders::PicturesLibrary->GetFileAsync( "3.jpg" )).
    			then([](task<Windows::Storage::StorageFile^>gettask )->String^
    		{
    			try
    			{
    				StorageFile^ file = dynamic_cast<StorageFile^>(gettask.get());
    				String^ val2;
    				val2 = file->FileType;
    				
    				return val2;
    				
    			}
    			catch( Exception^ e )
    			{
    			}
    		});
    		return getfile.get();
    	});
    }

     


    Best regards,
    Jesse


    Jesse Jiang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by its_me_here Tuesday, January 15, 2013 11:58 AM
    Tuesday, January 15, 2013 7:31 AM
  • Thanks, Jesse! That helped..Also now, I understand how to specify return statemenet from tasks..
    Tuesday, January 15, 2013 12:02 PM