2015-10-19 3 views
0

У меня есть код, который должен обновить пользовательский интерфейс, а затем дождаться его завершения (обновление может включать анимацию), а затем продолжить. Есть ли способ вызвать Application.Current.Dispatcher.Invoke(new Action (()=> { PerformUpdateWithAnimations(); } синхронным способом?WPF ждет завершения пользовательского интерфейса

Это общий вид:

List<thingsThatMove> myThings = new List<ThingsThatMove>(); 

//normal code interacting with the data 
// let's name this part of code A 
foreach (thing t in myThings) 
{ 
    thing.currentPosition = SomePoint; 
    if(thing.wasRejectedBySystem) thing.needsToMove = true; 
} 



//As a result of A we have some impact in the UI 
//That may need some animations (let's call this bloc B) 
Application.Current.Dispatcher.Invoke(new Action(() => { 
    foreach(thing in myThings) 
     if(thing.needsToMove) 
       createNewAnimation(thing); 
})); 

//Here is some final code that needs the final position of some 
//of the elements, so it cannot be executed until the B part has 
// been finished. Let's call this bloc C 

updateInternalValues(myThings); 
cleanUp(); 

Я попытался герметизирующего B в BackgroundWoker. Установка B блока как DoWork и слушать completed но doent работать, так как B Blok «завершает» после того, как Application.Current.Dispatcher называется, а не после того, как диспетчер сам заканчивает все, что

Как я могу сделать C ждать пока все анимации в B не закончатся?

ответ

0

Вы можете использовать асинхра/ждать, чтобы позвонить asynchrnously и продолжить после того, как он был выполнен ..

private async void RefreshCode() 
{ 
    List<thingsThatMove> myThings = new List<ThingsThatMove>(); 

    //normal code interacting with the data 
    // let's name this part of code A 
    foreach (thing t in myThings) 
    { 
     thing.currentPosition = SomePoint; 
     if(thing.wasRejectedBySystem) thing.needsToMove = true; 
    } 

    //As a result of A we have some impact in the UI 
    //That may need some animations (let's call this bloc B) 
    await Application.Current.Dispatcher.InvokeAsync(new Action(() => { 
     foreach(thing in myThings) 
      if(thing.needsToMove) 
        createNewAnimation(thing); 
    })); 

    //Here is some final code that needs the final position of some 
    //of the elements, so it cannot be executed until the B part has 
    // been finished. Let's call this bloc C 

    updateInternalValues(myThings); 
    cleanUp(); 
} 
+0

если он начнет делегат Asyncrhonously ... не мой код продолжать идти, не дожидаясь его Конец ? Мне нужно подождать, пока он закончит, прежде чем держать на нем – javirs

+0

Да .. позвольте мне обновить новый ответ. –

+0

Есть ли способ добавить уровень пользовательского интерфейса к задаче? потому что внутри моей PerformUpdateWithAnimations у меня есть вызов Application.dispatcher.invoke – javirs

Смежные вопросы