locked
Tasks or asynchronous calls from other threads crash

    Question

  • Can someone from Microsoft tell me what's wrong with creating tasks inside other tasks or calling asynchronous functions in other threads or inside a task ? Simple things like showing a message dialog inside a task or showing a messagedialog from the callback of a different messagedialog throw exceptions or just crash. What's up with that ?

    Sample code :

    auto t = concurrency::create_async([]
    	{
    		MessageDialog^ msg = ref new MessageDialog( "Content");
    		UICommand^ Button0 = ref new UICommand( "Button0", ref new UICommandInvokedHandler( Win8MessageBoxCallback, &WIN8_MESSAGE_BOX_CALLBACK::CommandInvokedHandler) );
    		Button0->Id = 0;
    		msg->Commands->Append(Button0);
    		IAsyncOperation<IUICommand^> ^Op = msg->ShowAsync();				
    	});

    The above is created inside a Windows::UI::Xaml::Media::Imaging::SurfaceImageSource's constructor. If I simply remove the "create_async" call, it works flawlessly.

    EDIT : Found this thread where it suggests that after a tasks lambda you need to call

    },

    task_continuation_context::use_current());

    It solved me a weird case

    of license purchase attempt when done in a callback of a messagedialog but there's still cases where it doesn't work (like the sample above).

    EDIT2: After more hours of research I found a second solution to my crashes. To use create task wherever you have IAsyncOperation<T> as return type.  So instead of doing

    messagedialog->ShowAsync();

    I now do

    auto ShowTask = create_task( msg->ShowAsync() );

    and I can finally show messageboxes from another task. However, the sample code I gave above still doesn't work.

    Monday, January 28, 2013 10:48 PM

Answers


  • Hi,

    I think this issue caused by the ASTA and MTA's issue. The UI operator should be ran in the ASTA thread. Maybe your task is running in the MTA thread in DX, so that it throw the error.

    You can try to call
      CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent);

    First and update UI
    Please check http://msdn.microsoft.com/en-us/library/windows/apps/hh750290.aspx

    And the first solution, it is mean that the task should run in the ASTA ask_continuation_context::use_current

    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.

    • Proposed as answer by Jesse Jiang Thursday, January 31, 2013 2:50 AM
    • Marked as answer by Jesse Jiang Monday, February 04, 2013 2:36 AM
    Wednesday, January 30, 2013 7:21 AM