2014-04-15 3 views
0

Unity3d로 Windows Phone 게임을 만들고 UI 스레드에서 Unity 스레드의 메서드를 비동기 적으로 호출해야합니다.Dispatcher.BeginInvoke 메서드 두 번째 실행 후 '고정'

이 모든 것이 작동하지만 한 가지 특별한 방법으로 첫 번째 실행이 예상대로 실행되지만 두 번째 이후에는 게임을 잠그는 것처럼 보입니다.

private async static Task<String> ShowDescriptionProductListing() 
    { 
     var x = await CurrentApp.LoadListingInformationAsync(); 

     StringBuilder builder = new StringBuilder(); 

     builder.AppendFormat("{0}\n{1}", x.Description, 
            x.ProductListings.FirstOrDefault().Value); 

     return builder.ToString(); 
    } 
    public static void ShowDescrProduct() 
    { 
     string x = ShowDescriptionProductListing().Result; 
     MessageBox.Show(x); 

    } 

나는 선을 생각 :

var x = await CurrentApp.LoadListingInformationAsync(); 

가장 가능성이 범인이다, 그러나 나는 힘든 시간을 디버깅하는 데 문제가 있습니다.

'보유'클래스는 단결에 그 방법과 같이이다 :

public static class HelperClass 
{ 
    public static void ShowDescrProduct() 
    { 
      Dispatcherr.InvokeOnUIThread(Tests.ShowDescrProduct); //The method above 
    } 
} 

Dispatcherr (그래 내가 하하 네임 스페이스를 사용해야합니다) 그냥 UI 스레드 내 설정이 개 동작 특성을 보유하고 있습니다. 항상 성공 -

public void EnterUIThread(Action action) 
    { 
     Dispatcher.BeginInvoke(() => 
     { 
      action(); 
     }); 
    } 

    private void Unity_Loaded() 
    { 
     Dispatcherr.InvokeUIThread = EnterUIThread; //One of the actions I just 
                //mentioned being assigned the above 
                //method 
    } 

는 그리고 첫 번째 호출 후, 갇혀 얻을 것 같다 Dispatcher.BeginInvoke하기 위해 EnterUIThread 호출에 있습니다.

다소 혼란 스러울 정도입니다.

누구나 통찰력을 줄 수 있습니까? 사전

답변

1

에서

덕분에 당신은 비동기 작업에 Result를 호출하고 있습니다. 비동기 작업이 끝날 때까지 UI 스레드가 차단됩니다. 비동기 작업은 UI 스레드가 빈 상태가 될 때까지 기다려야 UI 스레드에서 계속되는 LoadListingInformationAsync을 예약 할 수 있습니다.

두 작업이 서로 기다리고 있습니다. 이중 자물쇠.

아니요이 작업을 마칠 때까지 기다리는 동안 UI 스레드를 차단해야합니다. await 대신 ShowDescrProductasync 메소드를 작성해야합니다.

+0

감사합니다. Servy! :) –

관련 문제