1

I am developing an app for windows phone. My question is how can I trigger an event after 2 seconds?

    Private Sub btn1_Click(sender As Object, e As RoutedEventArgs) Handles btn1.Click
    Dim input As String = txtinput.Text
    Dim last As Char = input(input.Length - 1)
    If last = "A" Then
        Dim final As String = input.Substring(0, input.Length - 1) & "B"c
        txtinput.Text = final.

      'start timer here
      'trigger an event after 2 seconds

    ElseIf last = "B" Then
        Dim final As String = input.Substring(0, input.Length - 1) & "C"c
        Dim tmr As TimeSpan = TimeSpan.FromSeconds(2)
        txtinput.Text = final

      'start timer here
      'trigger an event after 2 seconds


    Else
        txtinput.Text = input + "A"
    End If

 End Sub

I am using Visual Basic as my language in developing this. Any help would be much appreciated.

techBeginner
  • 3,792
  • 11
  • 43
  • 59
clydewinux
  • 515
  • 3
  • 9
  • 18
  • What UI framework are you developing under? Does it not have a timer? – Jeff B Nov 02 '12 at 13:37
  • I am using visual basic as my language in developing this. – clydewinux Nov 02 '12 at 13:41
  • Yes, but are you using WinForms, WPF, Silverlight, etc...? For example, if you are using WinForms, there's a Timer in your toolbox you can drag onto your form. Then you just call `.Start()` on it and handle it's `Tick` event. – Jeff B Nov 02 '12 at 13:44
  • oh sorry, i am using windows phone application in visual studio express 2012.. – clydewinux Nov 02 '12 at 13:47
  • 1
    see the post http://stackoverflow.com/questions/3265715/is-there-a-timer-control-for-windows-phone-7 – techBeginner Nov 02 '12 at 13:59

2 Answers2

2

declare dispatcherTimer inside the class

Dim WithEvents dt As System.Windows.Threading.DispatcherTimer

then create instance of dispatcherTimer whereever you want, set time span

dt = New System.Windows.Threading.DispatcherTimer()
dt.Interval = New TimeSpan(0, 0, 0, 0, 500) '' 500 Milliseconds
dt.Start()

and here is your handler

Private Sub dt_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles dt.Tick
    ' Do Stuff here.
End Sub

*converted code to VB from here, though I have not tested it..it may work for you..

Community
  • 1
  • 1
techBeginner
  • 3,792
  • 11
  • 43
  • 59
0

Maybe I just don't know the environment because I have never programmed for phones in .Net, but what about:

System.Threading.Thread.Sleep(2000)

Hope this helps

John Bustos
  • 19,036
  • 17
  • 89
  • 151
  • 1
    That might cause the application to "freeze" for 2 seconds if it is done on the UI thread. – Jeff B Nov 02 '12 at 14:34