LayerFactory causes QueuedTask.Run(...).Wait() to hang

1707
1
03-08-2017 03:50 PM
KirkKuykendall
Occasional Contributor III

The Wait() method on the Task returned by QueuedTask.Run hangs when I try to use LayerFactory.

If I use await, it works as expected.  Am I doing something wrong?

This works:

protected override async void OnClick()
{
    await QueuedTask.Run(()=> {
            LayerFactory.CreateGroupLayer(MapView.Active.Map, 0, "group layer");
            Debug.Print("layer created");
        });
    Debug.Print("done");
}

But this hangs:

protected override void OnClick()
{
    QueuedTask.Run(()=> {
            LayerFactory.CreateGroupLayer(MapView.Active.Map, 0, "group layer");
            Debug.Print("layer created");
        }).Wait();
    Debug.Print("done");
}

This works (suggesting LayerFactory is clogging the plumbing):

protected override void OnClick()
{
    QueuedTask.Run(() => {
        for (int i = 0; i < 5; i++)
        {
            Debug.Print("Sleeping " + i);
            System.Threading.Thread.Sleep(1000);
        }
        Debug.Print("waking");
    }).Wait();
    Debug.Print("done");
}
Tags (2)
0 Kudos
1 Reply
Wolf
by Esri Regular Contributor
Esri Regular Contributor

Hi Kirk,

 Your question is probably best answered in a Microsoft c# Forum because the answer is found in the way Microsoft's Task-based Asynchronous Pattern (TAP) works.  In general "Wait" will synchronously block until the task completes. So the current thread is literally blocked waiting for the task to complete. As a general rule, you should always use 'await' in the context of ArcGIS Pro SDK asynchronous coding.

 - Wolf

0 Kudos