Monthly Archives: October 2014

Simple C# Interlocked use case

Original code in a ViewModel for a Windows Phone application looked like this:

 private readonly object _lock = new object();
 private int _secondsToNextRefresh;
 public int SecondsToNextRefresh
 {
 get { return _secondsToNextRefresh; }
 set { _secondsToNextRefresh = value; RaisePropertyChanged(() => SecondsToNextRefresh); }
 }

 protected void ResetRefreshTimer()
 {
 if (_refreshTimer == null)
 {
  _refreshTimer = new Timer(arg =>
  {
    if (SecondsToNextRefresh == 0)
    {     
     GetData();
     SecondsToNextRefresh = GetSecondsToNextRefresh();
    }
    else
    { 
     ModifySecondsToNextRefresh(SecondsToNextRefresh - RefreshPeriodInSec);
    }
    },null, 0, RefreshPeriodInSec * Constants.MilliSecsPerSec);
  } 
 }

protected int ModifySecondsToNextRefresh(int value)
 { 
   lock (_lock)
  {
   SecondsToNextRefresh = value; 
  }
  return SecondsToNextRefresh;
}

Upon further inspection of the code, it appeared to me that I can take advantage of the higher performance C# Interlocked methods.

Changing the above code to:


_refreshTimer = new Timer(arg =>
{
 if (_secondsToNextRefresh == 0)
 {   
   secsToNextRefresh = GetSecondsToNextRefresh();
   GetData(); 
  }
  ModifySecondsToNextRefresh(secsToNextRefresh) 

},null, 0, RefreshPeriodInSec * Constants.MilliSecsPerSec);

protected int ModifySecondsToNextRefresh(int value)
{ 
  var valueToReturn = Interlocked.Exchange(ref _secondsToNextRefresh, value);
  RaisePropertyChanged(() => SecondsToNextRefresh);
  return valueToReturn;
}

Based on advise from Writing High Performance .NET Code, surely is the preferred locking mechanism in this scenario.

Advertisements

Using javascript libs without TypeScript definition

Say you wanted to use Telerik’s HTML5 Report Viewer  in your TypeScript definition files and you cannot find the TypeScript definition file.   You are probably doing:

module ViewModel {

export class ReportViewModel {

constructor() {
this.init();
}

init() {

$(() => {

var reportViewer = jQuery("#reportViewer1");
if (reportViewer) {
reportViewer.telerik_ReportViewer({
serviceUrl: "/api/reports/",
templateUrl: 'kendo/ReportViewer/templates/telerikReportViewerTemplate-8.1.14.804.html',
reportSource: {
report: "Telerik.Reporting.Examples.CSharp.ProductCatalog, CSharp.ReportLibrary",
parameters: {
CultureID: "en"
}
}
});
}

});
}
}
}

However, without the TypeScript definition file, Visual Studio will complain that telerik_ReportViewer is not a property of jQuery.

You have two options:

1. Create an ambient variable for jQuery


declare var jQuery;

export class ReportViewModel {

just before the export class definition.  The disadvantage of this method is that you lose all IntelliSense for jQuery in Visual Studio.

2. Second option is to cast the result of the jQuery selector to any as follows:


var reportViewer = <any>jQuery("#reportViewer1");
if (reportViewer) {

The advantage of this method is that you continue working as normal without losing intellisense for jQuery.