question

ansalc-1556 avatar image
0 Votes"
ansalc-1556 asked ·

Timer in Visual Basic

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

I am having issues implementing the information at https://docs.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.

windows-uwp
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

PeterFleischer-3316 avatar image
0 Votes"
PeterFleischer-3316 answered ·

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


· 1 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello Peter.


Thank you.


0 Votes 0 · ·