locked
How do I make a Windows Store App execute a few lines of code every millisecond in Visual Basic? RRS feed

  • Question

  • I'm making a game for the Windows Store using Visual Basic on Visual Studio 2013. I want to know how to make a function add a variable to another variable then change a text block every millisecond. If I do an endless Do...While/Until statement that freezes the application. How can I make this possible?
    Wednesday, October 30, 2013 12:20 AM

All replies

  • I think DispatchTimer's the class you are looking for:

    http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.dispatchertimer

    An example of VB.NET within MainPage

    -

    ' The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
    
    ''' <summary>
    ''' An empty page that can be used on its own or navigated to within a Frame.
    ''' </summary>
    Public NotInheritable Class MainPage
        Inherits Page
        Dim timer As DispatcherTimer
    
        Public Sub New()
            InitializeComponent()
            timer = New DispatcherTimer() With {.Interval = TimeSpan.FromMilliseconds(1)}
            AddHandler timer.Tick, AddressOf timer_tick
        End Sub
    
        Private Sub timer_tick(sender As Object, e As Object)
            tbResult.Text = DateTime.Now.ToString()
        End Sub
    
        Private Sub btnStart_Click(sender As Object, e As RoutedEventArgs) Handles btnStart.Click
            timer.Start()
        End Sub
    
        Private Sub btnEnd_Click(sender As Object, e As RoutedEventArgs) Handles btnEnd.Click
            timer.Stop()
        End Sub
    End Class
    

    -



    • Edited by Saar Shen Wednesday, October 30, 2013 1:07 AM Update code from C# to VB.NET.
    Wednesday, October 30, 2013 12:28 AM
  • I just want to add that you would be wasting processing cycles by updating the UI element that frequently (or trying to), as at most you would want the visual update to occur "at most" once per frame.. and then, only if the value requires changing.

    You may want to take a look at the following link regarding frame updates:

    http://evanl.wordpress.com/2009/12/06/efficient-optimal-per-frame-eventing-in-wpf/

    I know that this example is in C#, but the idea is sound and seeing that at 60fps, an update only occurs once every 16ms.. which could change based on device/platform (30fps on Surface RT or once every 32ms) and changes wildly depending on what is going on.

    if your game will be doing some heavy lifting once every ms, you may get a freeze like effect because your game is basically attempting to run so many cycles per frame, it can never process a frame update.

    Wednesday, October 30, 2013 5:39 AM