Answered by:
Async operations in one function under c++

Question
-
Hello,
can somebody help me to translate the following c# code to c++?
The problem is to merge the two async functions to a synchron function and return the result as a async result.
private async void Button_Click_2(object sender, RoutedEventArgs e) { System.Diagnostics.Debug.WriteLine("before BeadBytesFromFile"); byte[] b = await ReadBytesFromFile(); System.Diagnostics.Debug.WriteLine("after BeadBytesFromFile"); } private async static Task<byte[]> ReadBytesFromFile() { System.Diagnostics.Debug.WriteLine("start BeadBytesFromFile"); var stream = await KnownFolders.MusicLibrary.OpenStreamForReadAsync("testfile.bin"); byte[] b = new byte[stream.Length]; int readedBytes = await stream.ReadAsync(b, 0, (int)stream.Length); System.Diagnostics.Debug.WriteLine("end BeadBytesFromFile"); return b; }
u
- Edited by urs32 Friday, September 21, 2012 4:37 PM
- Moved by Matt SmallMicrosoft employee, Moderator Friday, September 21, 2012 8:09 PM C++ question (From:Building Windows Store apps with C# or VB )
Friday, September 21, 2012 4:36 PM
Answers
-
Hi,
Please try these codes
create_task(KnownFolders::MusicLibrary->GetFileAsync("testfile.bin")).then([this](task<StorageFile^> t) { try { StorageFile^ file = t.get(); create_task(FileIO::ReadBufferAsync(file)).then([this, file](task<IBuffer^> tt) { try { IBuffer^ buffer = tt.get(); DataReader^ dataReader = DataReader::FromBuffer(buffer); Array<unsigned char>^ value= ref new Array<unsigned char>(buffer->Length); dataReader->ReadBytes(value); } catch(COMException^ ex) { } }); } catch(COMException^ ex) { } });
Best regards,
JesseJesse Jiang [MSFT]
MSDN Community Support | Feedback to us
- Marked as answer by Jesse Jiang Thursday, September 27, 2012 2:56 AM
Monday, September 24, 2012 6:57 AM
All replies
-
Hi,
Please try these codes
create_task(KnownFolders::MusicLibrary->GetFileAsync("testfile.bin")).then([this](task<StorageFile^> t) { try { StorageFile^ file = t.get(); create_task(FileIO::ReadBufferAsync(file)).then([this, file](task<IBuffer^> tt) { try { IBuffer^ buffer = tt.get(); DataReader^ dataReader = DataReader::FromBuffer(buffer); Array<unsigned char>^ value= ref new Array<unsigned char>(buffer->Length); dataReader->ReadBytes(value); } catch(COMException^ ex) { } }); } catch(COMException^ ex) { } });
Best regards,
JesseJesse Jiang [MSFT]
MSDN Community Support | Feedback to us
- Marked as answer by Jesse Jiang Thursday, September 27, 2012 2:56 AM
Monday, September 24, 2012 6:57 AM -
Hello,
thank you for your answer.
I did not understand how i can handle the return value. In my example b (byte[]) and in your c++ code the buffer.
And a second question:
Is there a similar possibility how System.Diagnostics.Debug.WriteLine under c++?
thanks
u
Monday, September 24, 2012 7:05 PM -
You can use a loop to convert char array to byte[]
And the OutputDebugString API is the same function with System.Diagnostics.Debug.WriteLineIBuffer^ buffer = tt.get(); DataReader^ dataReader = DataReader::FromBuffer(buffer); Array<unsigned char>^ value= ref new Array<unsigned char>(buffer->Length); dataReader->ReadBytes(value); byte bytes[255]; for(int i=0;i<value->Length;i++) { bytes[i]=value->get(i); } OutputDebugString(L"Read buffer completed");
Best regards,
JesseJesse Jiang [MSFT]
MSDN Community Support | Feedback to us
- Proposed as answer by chris_vr Tuesday, September 25, 2012 2:38 PM
Tuesday, September 25, 2012 5:45 AM