Timer for Silverlight 1.1

August 13, 2007 at 10:27 am | Posted in Silverlight | 3 Comments

I needed a timer control for a Silverlight 1.1 application but it is not supported in the Alpha release.  My solution was to use the timer in Javascript and have it send events to the C# code.

Event sending from Javascript to C# is described in the Silverlight QuickStart document.

Here is the initialization of the timer in Javascript:

//initialize silverlight event hookup and start timer 
var control; 
function init(sender){ 
control = sender; 
control.Content.basic.AgEvents = agEventHandler; 

In this routine, the timer is set to execute the jtimer function every 10 ms.  (The agEvents statement is used for sending information to the Javascript.  I will describe this later.)

The init function is called by the CreateSilverlight function in the page.js file:

// JScript source code 

//contains calls to silverlight.js, example below loads Page.xaml 
function createSilverlight() 
        source: "Page.xaml", 
        parentElement: document.getElementById("SilverlightControlHost"), 
        id: "SilverlightControl", 
        properties: { 
            width: "100%", 
            height: "100%", 
            version: "1.1", 
            enableHtmlAccess: "true", 
            isWindowless: "True" 
        events: { 
        onLoad: init 

    // Give the keyboard focus to the Silverlight control by default 
    document.body.onload = function() { 
      var silverlightControl = document.getElementById('SilverlightControl'); 
      if (silverlightControl) 

Here is the jtimer function:

//Send position of player to silverlight based on a timer 
function jtimer(){ 

In this function, a function timer is called in the C# code with a value that is passed as an argument.

On the C# side, the timer function looks like this:

        //This receives the timer event from Javascript 
        public void timer(double arg) 
            tnow = arg;

Note the ‘[Scriptable]’ tag which makes the timer function usable from Javascript.

Finally, the page load event needs some boilerplate to hook the Javascript to C# communications:

        public void Page_Loaded(object o, EventArgs e) 
            // Required to initialize variables 
            //Register the scriptable endpoints 
            WebApplication.Current.RegisterScriptableObject("basic", this);



RSS feed for comments on this post. TrackBack URI

  1. In the alpha release of Silverlight there is a class called htmltimer. did you try using that?

  2. Vivek:

    My application is a port from a mostly-Javascript asp.Net program where I used the Javascript timer. I only recently learned about an ‘htmltimer’ but heard that is was ‘low resolution’. I need to have timer updates at least every 10-20 ms. Since the Javascript timer works well for this, I have not investigated the htmltimer further.

  3. got it. Low resolution part is something we will fix in the future release.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Create a free website or blog at WordPress.com.
Entries and comments feeds.

%d bloggers like this: