locked
Throw Exception in C++ App to call async method WinRT Component wirtten by C# RRS feed

  • Question

  • Hi All,

    My environment is Windows 8 Pro RTM  and VS 2012 RTM.

    I have wirtten a WinRT Component by C# to access websites and to get the text content. I use C# app to call the async method which worked well. But when I use C++ app to call the method, it will throw an Exception:

    +  &ThisException 0x0283e080 {ExceptionCode=0xe06d7363 ExceptionFlags=0x00000001 ExceptionRecord=0x00000000 {ExceptionCode=...} ...} EHExceptionRecord *

    Below is my code. Would you please tell me the right way using C++ to call WinRT async method?

    Thank you for your attention and I am looking forward to your reply.

    WinRT Component: Service

    namespace RtComponent
    {
        internal class MyService
        {
            HttpClient httpClient;
    
            internal async Task<String> GetText()
            {
                httpClient = new HttpClient();
    
                HttpResponseMessage response = await httpClient.GetAsync("http://www.microsoft.com");
    
                String text = await response.Content.ReadAsStringAsync();
    
                return text;
            }
        }
    }

    WinRT Component: Feature

    namespace RtComponent
    {
        public sealed class MyFeature
        {
            public IAsyncOperation<String> GetText()
            {
    
                Service service = new Service();
                return Task.Run<String>(async () =>
                    {
                        var data = await service.GetText();
                       
                        return data;                 
                    }).AsAsyncOperation();
            }
         }
    }
    C# App : Work well

                MyFeature mf = new MyFeature();
    
                string str = await mf.GetText();

    C++ App : Throw Excepiton

    	IAsyncOperation<String^>^ Op = lb->GetText();
    	String^ str = Op->GetResults();



    • Edited by Ma Ning Sunday, August 19, 2012 10:14 AM
    Sunday, August 19, 2012 10:09 AM

Answers

  • Try these codes

     

    create_task(lb->GetText()).then([](String^ str)

        {      

           String^ res=str;

        });

     

     

    Best regards,

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Ma Ning Tuesday, August 21, 2012 2:41 PM
    Monday, August 20, 2012 10:49 AM

All replies