locked
Wait a task's problem

    Question

  • For example my code is below:

    task<StorageFile^> openTask(Windows::Storage::ApplicationData::Current->LocalFolder->GetFileAsync("1.xml"));
    try
    {
      openTask
    .wait();
     
    StorageFile^ File = openTask.get();
     
    XmlDocument^ xmlFilell = ref new XmlDocument();
    task
    <XmlDocument^> xmlFile (xmlFilell- >LoadFromFileAsync(File));
    try{
    xmlFile
    .wait();
    XmlDocument^ GetxmlFile = xmlFile.get();
     
    }
    catch(Exception^ e)
     
    {
      
    }
    }
    catch(Exception^ e)
    {
    }

    why it always break up at the wait meature,sometime in the first wait,and sometime in the second wait

    What caused this?

    Monday, December 24, 2012 10:24 AM

Answers

  • I think main thread means UI thread . I hope you know there is no way to create a new thread in Windows Store Apps. Here you are encouraged for using ThreadPool . So you can submit your task in threadpool. Here is a sample code

    auto taskDelegate = [=](Windows::Foundation::IAsyncAction^ workItem)
    	{
               // Some code which you would like to execute in a worker thread
    	
    	};
    
    	auto workItemHandler = ref new Windows::System::Threading::WorkItemHandler( taskDelegate);
    	Windows::Foundation::IAsyncAction^ threadPoolWorkItem = Windows::System::Threading::ThreadPool::RunAsync(workItemHandler, Windows::System::Threading::WorkItemPriority::Normal);

    For more information you can follow this link

    Using the thread pool in Windows Store apps

    Thread pool sample

    • Marked as answer by Jesse Jiang Thursday, December 27, 2012 6:12 AM
    Wednesday, December 26, 2012 4:44 AM

All replies

  • Check whether you are executing your code from a worker thread or not, because  UI thread does not allow any blocking. As Windows Store Apps are completely asynchronous and UI always remains responsive, blocking or waiting is not expected here. You may use call-back mechanism in this context.

    If you are executing your code from UI thread you should use task chain for maintaining sequence, because UI thread does not allow any blocking for synchronization.

    task chains, -> myTask.then(…).then(…).then(…);

    If you are executing your code in a different worker thread, you may try like following sample

    create_task( fileHandle->OpenAsync(Windows::Storage::FileAccessMode::Read) ).then(
            [&taskResult] (task<void> previousTask)
        {
            try
            {
                previousTask.get();  // get exception
            }
            catch (Exception^ exception)
            {
                taskResult = exception->HResult;
            }
        }).get();         // Also try .wait()
    If you have multiple asynchronous operations, try to use task chain.
    Monday, December 24, 2012 3:44 PM
  • Thanks!

    I know why it breaks up,because I call it in the main thread,I need to create a new thread,Is there any sample about this ?

    Tuesday, December 25, 2012 10:36 AM
  • I think main thread means UI thread . I hope you know there is no way to create a new thread in Windows Store Apps. Here you are encouraged for using ThreadPool . So you can submit your task in threadpool. Here is a sample code

    auto taskDelegate = [=](Windows::Foundation::IAsyncAction^ workItem)
    	{
               // Some code which you would like to execute in a worker thread
    	
    	};
    
    	auto workItemHandler = ref new Windows::System::Threading::WorkItemHandler( taskDelegate);
    	Windows::Foundation::IAsyncAction^ threadPoolWorkItem = Windows::System::Threading::ThreadPool::RunAsync(workItemHandler, Windows::System::Threading::WorkItemPriority::Normal);

    For more information you can follow this link

    Using the thread pool in Windows Store apps

    Thread pool sample

    • Marked as answer by Jesse Jiang Thursday, December 27, 2012 6:12 AM
    Wednesday, December 26, 2012 4:44 AM
  • Thanks, I have got it!
    Wednesday, December 26, 2012 8:25 AM
  • Hi bing_g,

    If you have got your answer you should Marked/Vote it.

    Wednesday, December 26, 2012 8:59 AM