Timer in Visual Basic

asked 2020-05-01T09:22:48.06+00:00
ansalc 436 Reputation points

What is the code for a timer in UWP Visual Basic ?

I am having issues implementing the information at https://learn.microsoft.com/en-us/windows/uwp/threading-async/use-a-timer-to-submit-a-work-item

I only need to launch the timer at a certain point of my app and run a procedure when the timer reaches a given elapsed time.

Thanks.

Universal Windows Platform (UWP)
No comments
{count} votes

Accepted answer
  1. answered 2020-05-02T11:08:59.247+00:00
    Peter Fleischer (Freelancer) 17,396 Reputation points

    Hi, try following MVVM demo:

    XAML:

    <Page
        x:Class="UWP10App1VB.Page18"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:UWP10App1VB.UwpApp18"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
      <Page.DataContext>
        <local:ViewModel/>
      </Page.DataContext>
      <StackPanel>
        <Button Content="Start Timer" Command="{Binding Cmd}"/>
        <TextBlock Text="{Binding Info}"/>
      </StackPanel>
    </Page>
    

    ViewModel:

    Namespace UwpApp18
    
      Public Class ViewModel
        Implements INotifyPropertyChanged
    
        Private _info As String
        Public Property Info As String
          Get
            Return Me._info
          End Get
          Set(value As String)
            Me._info = value
            OnPropertyChanged()
          End Set
        End Property
    
        Public Property Cmd As ICommand = New RelayCommand(AddressOf CmdExec)
    
        Private Sub CmdExec(obj As Object)
          Dim sc = SynchronizationContext.Current
          Info = "Timer will be startet"
          Dim DelayTimer = ThreadPoolTimer.CreateTimer(Sub(source)
                                                         ' do work
                                                         sc.Post(New SendOrPostCallback(Sub(state)
                                                                                          Info = "Timer ended"
                                                                                        End Sub), Nothing)
                                                       End Sub, TimeSpan.FromMinutes(1))
        End Sub
    
        Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
        Friend Sub OnPropertyChanged(<CallerMemberName> Optional propName As String = "")
          RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propName))
        End Sub
      End Class
    
    End Namespace
    

0 additional answers

Sort by: Most helpful