• 0

[C#][XMAL] Updating the app


Question

I am trying to build a Windows 8 metro style app using C# and XMAL is there a way to call a methord X times a second instead of when clicked , when touched est.

For example

when user hits a button X , a count down will appear on a screen , which will count down from 10 to 0.

Dont worry about getting the count down to appear just how to count down a int

int countDown;

Void UPDATE()

{

countDown --;

if (countDown == 600)

}

that type of thing.

Everything i have seen on MSDN was on button press , event stuff.

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

Thanks what about more advanced stuff instead of just a clock, What if i want to just say call a method from the very start and keep calling it forever?

Just like Update in XNA?

My situation im building a calculate i have a number of buttons , i want to show the current equsaion the user has imputed , just say 5 + 7 * 4 + 6.

I can call a method every time for every button , but why not something that gets called once every update. or can you do it once evey "button press".

Link to comment
Share on other sites

  • 0

A couple of things:

- You'll likely want to databind the count down value to a property, and use the INotifyPropertyChanged interface to automatically reflect your back end changes to your front end view

- You'll want to maintain UI thread affinity. If you can guarantee you'll raise the initial method on the UI thread (which you can because its a button click) then it makes sense to await a Task because it captures the ambient SynchronizationContext and saves you the UI thread marshaling.

Id do something like

public async Task Countdown()
{
	   SomeCountDownProperty = 10;
	   while (SomeCountdownProperty != 0)
	   {
			  await Task.Delay(1000);
			  SomeCountDownProperty--;
	   }
}

Then on your button click event simply do:

await Countdown();

SomeCountdownProperty is a property which raises a PropertyChanged event when the value changes. It is always raised on the UI thread because the continuation is always run on the current synchronization context (which is CoreDispatcher because we are raised from a Click handler)

Link to comment
Share on other sites

This topic is now closed to further replies.