当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - How to Pause Before Performing an Action

问题描述:

I have the need to place a quick pause in my application before performing an action (enabling an application bar button), but I am unsure of how to best accomplish this. Basically a lot of processing is happening in another thread but then the UI is updated. Once the UI is updated, I am scrolling a pivot control to a particular pivot item. I would like to pause for about 1 second, or however long it takes to scroll to the previous pivot item in the pivot control, before allowing the application bar button to be enabled. How might I accomplish this? What I have so far is as follows

// Show image and scroll to start page if needed

if (Viewport != null)

{

Viewport.Source = result;

if (editPagePivotControl != null && editPagePivotControl.SelectedIndex != 0)

{

//Here is where the pivot control is told to move to the first item

// (from the second which it will be on before this happens)

editPagePivotControl.SelectedIndex = 0;

}

//A flag to determine whether the app bar button should be enabled

//How to pause for the time it takes to finish moving

// to the first pivot item?

if (_wasEdited)

((ApplicationBarIconButton)ApplicationBar.Buttons[0]).IsEnabled = true;

}

网友答案:

Try this, using System.Windows.Threading.DispatcherTimer:

if (_wasEdited)
{
   DispatcherTimer t = new DispatcherTimer(DispatcherPriority.Normal, Dispatcher);
   t.Tick += new EventHandler((o,e) => 
     ((ApplicationBarIconButton)ApplicationBar.Buttons[0]).IsEnabled = true;
   t.Interval = TimeSpan.FromMilliseconds(1000);
   t.Start();
}

This will wait for 1000 milliseconds, then come back to the UI thread (since we set Dispatcher in the DispatcherTimer constructor) and enable your application bar.

If you do this a lot, consider making the timer a member of the class.

分享给朋友:
您可能感兴趣的文章:
随机阅读: