locked
Has anybody tried to use System.Threading.Timer in Lightswitch RRS feed

  • Question

  • Hi, 

    I need to do some periodical job in my lightswitch application and the most proper way to run it is via System.Threading.Timer object. But when I implement it:

    System.Threading.Timer t = new System.Threading.Timer((object data) => { do something... ; }, null, 3000, 3000); 

    the timer is never triggered. I put break point on the line where I instantiate Timer object (the line abowe) and the timer is instantiated, but it target method has never been invoked.

    I put the same code in several places inside lightsiwtch application and not only in one application, but in several lightswitch applications, but target method is never triggered. 

    But when I put the same code for timer in console application, windows forms application or silverlight application it works fine. Has anybody experienced the same problem and what is the solution? 

    Thanks in advance!

    Thursday, October 11, 2012 1:33 PM

Answers

  • Here is the code I use to refresh my screen hopefully you'll see something you missed. (Originally written in VB.NET, used online convert to get C# so take the syntax with a grain of salt):

    VB.NET

    Imports System.Windows.Threading
    Imports Microsoft.LightSwitch.Threading
    
    Namespace LightSwitchApplication
    
        Public Class AppName
    
            'Create a timer to refresh screen
            Private Shared ReadOnly Timer As New DispatcherTimer()
    
            Private Sub AppName_InitializeDataWorkspace(saveChangesTo As System.Collections.Generic.List(Of Microsoft.LightSwitch.IDataService))
                'Initialize Timer for refreshing the screen
                Dispatchers.Main.BeginInvoke(Sub()
                                                 AddHandler Timer.Tick, AddressOf Each_Tick
                                                 Timer.Interval = New TimeSpan(0, 0, 1)
                                             End Sub)
            End Sub
    
            Private Sub Each_Tick(ByVal sender As Object, ByVal e As EventArgs)
                        'Other code that gets executed each tick of the Timer
                        '
                        '
                        '
    
                        'Stop Timer for refreshing the screen
                        Dispatchers.Main.BeginInvoke(Sub()
                                                         Timer.Stop()
                                                     End Sub)
            End Sub
    
            Private Sub ButtonName_Execute()
                    'Start Timer for refreshing the screen
                    Dispatchers.Main.BeginInvoke(Sub()
                                                     Timer.Start()
                                                 End Sub)
            End Sub
    
        End Class
    
    End Namespace
    

    C#

    using Microsoft.LightSwitch.Threading;
    using System.Windows.Threading;
    
    namespace LightSwitchApplication {
        
        public class AppName {
            
            private DispatcherTimer Timer = new DispatcherTimer();
            
            private void AppName_InitializeDataWorkspace(System.Collections.Generic.List[] saveChangesTo, void Of, void Microsoft.LightSwitch.IDataService) {
                // Initialize Timer for refreshing the screen
                Dispatchers.Main.BeginInvoke(Sub, (Unknown, AddHandler, Timer.Tick, new System.EventHandler(this.Each_Tick), Timer.Interval=NewTimeSpan(0,0,1Unknown, End, Sub);
            }
            
            private void Each_Tick(object sender, EventArgs e) {
                // Other code that gets executed each tick of the Timer
                // 
                // 
                // 
                // Stop Timer for refreshing the screen
                Dispatchers.Main.BeginInvoke(Sub, (Unknown, Timer.Stop(), End, Sub);
            }
            
            private void ButtonName_Execute() {
                // Start Timer for refreshing the screen
                Dispatchers.Main.BeginInvoke(Sub, (Unknown, Timer.Start(), End, Sub);
            }
        }
    }


    • Marked as answer by Angie Xu Friday, October 19, 2012 9:11 AM
    Thursday, October 11, 2012 2:22 PM

All replies

  • Here is the code I use to refresh my screen hopefully you'll see something you missed. (Originally written in VB.NET, used online convert to get C# so take the syntax with a grain of salt):

    VB.NET

    Imports System.Windows.Threading
    Imports Microsoft.LightSwitch.Threading
    
    Namespace LightSwitchApplication
    
        Public Class AppName
    
            'Create a timer to refresh screen
            Private Shared ReadOnly Timer As New DispatcherTimer()
    
            Private Sub AppName_InitializeDataWorkspace(saveChangesTo As System.Collections.Generic.List(Of Microsoft.LightSwitch.IDataService))
                'Initialize Timer for refreshing the screen
                Dispatchers.Main.BeginInvoke(Sub()
                                                 AddHandler Timer.Tick, AddressOf Each_Tick
                                                 Timer.Interval = New TimeSpan(0, 0, 1)
                                             End Sub)
            End Sub
    
            Private Sub Each_Tick(ByVal sender As Object, ByVal e As EventArgs)
                        'Other code that gets executed each tick of the Timer
                        '
                        '
                        '
    
                        'Stop Timer for refreshing the screen
                        Dispatchers.Main.BeginInvoke(Sub()
                                                         Timer.Stop()
                                                     End Sub)
            End Sub
    
            Private Sub ButtonName_Execute()
                    'Start Timer for refreshing the screen
                    Dispatchers.Main.BeginInvoke(Sub()
                                                     Timer.Start()
                                                 End Sub)
            End Sub
    
        End Class
    
    End Namespace
    

    C#

    using Microsoft.LightSwitch.Threading;
    using System.Windows.Threading;
    
    namespace LightSwitchApplication {
        
        public class AppName {
            
            private DispatcherTimer Timer = new DispatcherTimer();
            
            private void AppName_InitializeDataWorkspace(System.Collections.Generic.List[] saveChangesTo, void Of, void Microsoft.LightSwitch.IDataService) {
                // Initialize Timer for refreshing the screen
                Dispatchers.Main.BeginInvoke(Sub, (Unknown, AddHandler, Timer.Tick, new System.EventHandler(this.Each_Tick), Timer.Interval=NewTimeSpan(0,0,1Unknown, End, Sub);
            }
            
            private void Each_Tick(object sender, EventArgs e) {
                // Other code that gets executed each tick of the Timer
                // 
                // 
                // 
                // Stop Timer for refreshing the screen
                Dispatchers.Main.BeginInvoke(Sub, (Unknown, Timer.Stop(), End, Sub);
            }
            
            private void ButtonName_Execute() {
                // Start Timer for refreshing the screen
                Dispatchers.Main.BeginInvoke(Sub, (Unknown, Timer.Start(), End, Sub);
            }
        }
    }


    • Marked as answer by Angie Xu Friday, October 19, 2012 9:11 AM
    Thursday, October 11, 2012 2:22 PM
  • Thank you. 

    System.Windows.Threading.DispatcherTimer will do the job. It would be interesting for me to know does anybody else have the problem with System.Threading.Timer in Lightswitch, but your post allows me to solve what I want. Tnanks.

    Thursday, October 11, 2012 3:16 PM