Window Support

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Monday, 27 July 2009

Silverlight 3 : Analytics Class Capabilities

Posted on 02:28 by Unknown

I hope my last article on Out of Browser breaking changes was useful for you, Today I am going to talk about the Analytics Class provided by Silverlight 3 with a very small demo. For this, you don’t need anything special since Analytics class is already made available as built-in once you install Silverlight 3.

About Analytics Class :

You will find this under System.Windows Namespace,So you don’t need any other references to set inside your project beside System.Windows.This class exposes two main properties as :

  • AverageProcessorLoad - Gets how much of the CPU this process is using across all cores averaged together (This might be directly applicable for Multi-core processors environment,mine is Centrino machine and not Core 2, but results are averaged as per definition)
  • AverageProcessLoad - Gets how much CPU processing is being used across all cores averaged together.

Another one is there known as GPUCollection which is though not part of my demo, but it is used to get a collection of GpuInformation objects which each include details taken from a video driver. The collection is useful for multi-adapter cases. For more information on Analytics Class, kindly refer Silverlight Offline documentation.

In my demo, I have also used some of the properties of Media Element Control which generally people don’t use since for scenarios of just playing videos they might not be applicable. Those properties are self-explanatory so again refer documentation if you need any more information. Now our Demo

XAML Code :

<Grid x:Name="LayoutRoot" Background="White">
       <MediaElement x:Name="MyMedia" Height="154" AutoPlay="True" Source="Wildlife.wmv" Margin="3,8,279,0" VerticalAlignment="Top" />
       <Rectangle Stroke="Black" Margin="6,166,8,8">
           <Rectangle.Fill>
               <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                   <GradientStop Color="Black" Offset="0"/>
                   <GradientStop Color="#FF99ABFF" Offset="1"/>
                   <GradientStop Color="#FFF8EAFA"/>
               </LinearGradientBrush>
           </Rectangle.Fill>
       </Rectangle>
       <dataInput:Label x:Name="compcpu" HorizontalAlignment="Left" Margin="19,193,0,0" Width="120" Content="Computer CPU Usage" Height="20" VerticalAlignment="Top"/>
       <dataInput:Label x:Name="slcpu" HorizontalAlignment="Right" Margin="0,0,509,185" VerticalAlignment="Bottom" Content="Silverlight CPU Usage"/>
       <ProgressBar x:Name="pgbar1" Margin="154,193,286,0" Height="20" VerticalAlignment="Top"/>
       <ProgressBar x:Name="pgbar2" Margin="154,0,286,184" Height="20" VerticalAlignment="Bottom"/>
       <dataInput:Label x:Name="gpucpu" HorizontalAlignment="Left" Margin="21,0,0,154" VerticalAlignment="Bottom" Content="Download Progress"/>
       <ProgressBar x:Name="pgbar3" Height="20" Margin="155,0,285,150" VerticalAlignment="Bottom"/>
       <dataInput:Label HorizontalAlignment="Left" Margin="23,0,0,116" VerticalAlignment="Bottom" Content="Audio Stream Count :"/>
       <dataInput:Label HorizontalAlignment="Left" Margin="23,0,0,97" VerticalAlignment="Bottom" Height="13" Content="Buffering Progress :"/>
       <dataInput:Label HorizontalAlignment="Left" Margin="23,0,0,72" VerticalAlignment="Bottom" Content="Buffering Time :"/>
       <dataInput:Label HorizontalAlignment="Left" Margin="23,0,0,50" VerticalAlignment="Bottom" Content="Dropped Frame Per Second :"/>
       <dataInput:Label x:Name="txtBufferingTime" HorizontalAlignment="Left" Margin="212,0,0,71" VerticalAlignment="Bottom"/>
       <dataInput:Label x:Name="txtBufferingProgress" HorizontalAlignment="Left" Margin="213,0,0,92" VerticalAlignment="Bottom"/>
       <dataInput:Label x:Name="txtAudioStreamCount" HorizontalAlignment="Left" Margin="213,0,0,114" VerticalAlignment="Bottom"/>
       <dataInput:Label x:Name="txtDFPS" HorizontalAlignment="Left" Margin="212,0,0,49" VerticalAlignment="Bottom"/>
       <dataInput:Label Margin="0,0,238,26" VerticalAlignment="Bottom" HorizontalAlignment="Right" Content="Natural Video Width :"/>
       <dataInput:Label Margin="0,0,238,49" VerticalAlignment="Bottom" HorizontalAlignment="Right" Content="Natural Video Height :"/>
       <dataInput:Label Margin="0,0,238,116" VerticalAlignment="Bottom" HorizontalAlignment="Right" Content="Natural Duration :"/>
       <dataInput:Label Margin="23,0,0,26" VerticalAlignment="Bottom" Content="Current State :" HorizontalAlignment="Left"/>
       <dataInput:Label x:Name="txtCurrentState" Margin="213,0,0,25" VerticalAlignment="Bottom" HorizontalAlignment="Left"/>
       <dataInput:Label x:Name="txtNtrlDur" Margin="0,0,94,115" VerticalAlignment="Bottom" HorizontalAlignment="Right"/>
       <dataInput:Label x:Name="txtNVH" Margin="0,0,94,46" VerticalAlignment="Bottom" HorizontalAlignment="Right"/>
       <dataInput:Label x:Name="txtNVW" Margin="0,0,94,23" VerticalAlignment="Bottom" HorizontalAlignment="Right"/>
   </Grid>

C# Code :

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Silverlight3NewFeature
{
    public partial class Page : UserControl
    {
        Analytics analytics;

        public Page()
        {
            InitializeComponent();
            CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);           
        }

        void CompositionTarget_Rendering(object sender, EventArgs e)
        {
            if (analytics == null)
                analytics = new Analytics();
                pgbar1.Value = analytics.AverageProcessorLoad;
                pgbar2.Value = analytics.AverageProcessLoad;
                pgbar3.Value = MyMedia.DownloadProgress;

                txtAudioStreamCount.Content = MyMedia.AudioStreamCount;
                txtBufferingTime.Content = MyMedia.BufferingTime;
                txtBufferingProgress.Content = MyMedia.BufferingProgress;
                   txtDFPS.Content = MyMedia.DroppedFramesPerSecond;
                txtCurrentState.Content = MyMedia.CurrentState;
                txtNtrlDur.Content = MyMedia.NaturalDuration;
                txtNVH.Content = MyMedia.NaturalVideoHeight;
                txtNVW.Content = MyMedia.NaturalVideoWidth;

        }
    }
}

A surprise for few of you for that CompositionTarget_Rendering Event, Well CompositionTarget exposes one and only one event which we are using and which occurs when the core Silverlight rendering process renders a frame, Since both the properties of Analytics class which we are using are directly reflecting value which can get change at each instance (i.e CPU and Process Load). So Loaded event might not be a suitable event for such requirement. Please note that, I am aware that values from Analytics class returns float value and not complete integer, But my idea is to show overall average consumption, so I opt ProgressBar control and not label.

Few of you may not be impress by so many labels and may wish to print all values on some TextBlock under some stackpanel, Well then this code might be good for you :

String str;

str = "Audio Stream Count :" + MyMedia.AudioStreamCount + "\n"
    + "Buffering Progress :" + MyMedia.BufferingProgress + "\n"
    + "Buffering Time :" + MyMedia.BufferingTime + "\n"
    + "Dropped Frame Per Second :" + MyMedia.DroppedFramesPerSecond + "\n"
    + "Current State :" + MyMedia.CurrentState + "\n"
    + "Natural Duration :" + MyMedia.NaturalDuration + "\n"
    + "Natural Video Height :" + MyMedia.NaturalVideoHeight + "\n"
    + "Natural Video Width :" + MyMedia.NaturalVideoWidth + "\n";

and then assign that str to any of TextBlock as Text property.

Output will look like this :

SLAnalytics

I hope this small demo will help you to understand the capabilities of Analytics class in Silverlight 3, I will be back with much more soon.

Vikram.

Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • First Windows Phone 7 update February 2011 - Small update but Big start
    After tons of rumors and set of predictions on Windows Phone 7 all over Internet, Microsoft came up with first Windows Phone 7 minor update ...
  • The little Story of “I Unlock Joy” event by Microsoft and Pune User Group
      This post is about recent “I Unlock Joy” event happened in Pune which was conducted by Microsoft and Pune User Group. Little History : ...
  • Silverlight 2 : Theming from Silverlight 2 Toolkit
    It’s now time to put on some cool Themes to your Silverlight 2 Controls. I have already talked about Silverlight toolkit in my last post, ...
  • DeepZoom with PhotoZoom and Silverlight.live.com
    I have already wrote article on How to build DeepZoom with DeepZoom Composer, I know after that another version of DeepZoom came up, but cha...
  • Silverlight 5 : Platform Invoke (PInvoke) in Silverlight
      Two days back Microsoft announced availability of Silverlight 5 RC,I encourage you to download bits from here , My friend Pete Brown alr...
  • Silverlight On Mobile : Windows Phone 7 Splash Screen and Customization
    After talking about 3D capabilities on Windows Phone 7 using Silverlight in last article , Now I am moving ahead with small but equally impo...
  • Silverlight, HTML5 & Windows 8 : Where we are heading to ?
    This is not the post or yet another post on most happening debate of Silverlight and HTML5, This is just a visit to all of them to realize t...
  • Silverlight 3 : Out Of Browser
    I have already written an article over Silverlight Out Of Browser functionality few months back ( http://pendsevikram.blogspot.com/2009/04/s...
  • Mango : Using DeviceStatus in Windows Phone 7.1
    First of all “Thank You” for your wonderful response and comments on my last article on Silverlight Vs HTML5 ,I hope you like the points I ...
  • Share Status and Link on Socials in Windows Phone
      Hope you all doing good.Sorry for the great delay here,I am almost working for all 7 days in week and that too on SQL Server – SSIS whic...

Blog Archive

  • ►  2013 (4)
    • ►  August (1)
    • ►  April (3)
  • ►  2012 (4)
    • ►  July (1)
    • ►  March (2)
    • ►  January (1)
  • ►  2011 (24)
    • ►  December (1)
    • ►  September (4)
    • ►  August (2)
    • ►  July (1)
    • ►  June (4)
    • ►  May (3)
    • ►  April (3)
    • ►  March (1)
    • ►  February (4)
    • ►  January (1)
  • ►  2010 (21)
    • ►  December (1)
    • ►  November (2)
    • ►  October (3)
    • ►  September (2)
    • ►  August (4)
    • ►  July (5)
    • ►  May (1)
    • ►  April (1)
    • ►  March (1)
    • ►  January (1)
  • ▼  2009 (49)
    • ►  December (1)
    • ►  November (5)
    • ►  October (2)
    • ►  September (1)
    • ►  August (5)
    • ▼  July (5)
      • Silverlight 3 : Analytics Class Capabilities
      • Silverlight 3 : Out Of Browser
      • What’s going on in RIA World ?
      • Welcome to Silverlight 3 RTW and Blend 3 + Sketch ...
      • I am Silverlight MVP !!!
    • ►  June (1)
    • ►  May (5)
    • ►  April (5)
    • ►  March (9)
    • ►  February (4)
    • ►  January (6)
  • ►  2008 (43)
    • ►  December (3)
    • ►  November (9)
    • ►  October (7)
    • ►  September (4)
    • ►  August (2)
    • ►  July (3)
    • ►  June (4)
    • ►  May (3)
    • ►  March (3)
    • ►  February (5)
Powered by Blogger.

About Me

Unknown
View my complete profile