Category Archives: Uncategorized

From BackgroundWorker, to TPL, to Async

I’m back to blogging again.  More on that later…

In any client UI code (WinForms/WPF/Silverilght/WinRT), it’s always a good practice to do as much in the background as possible so you aren’t blocking the UI thread.  “In the old days,” the BackgroundWorker was my best friend when I needed to executed some code in the background.  Along came the Task Parallel Library (TPL), and I moved to using it over Background worker.  Now we have the async enhancements coming soon.  Here’s a comparison of the approaches.

Consider this UI:

   1:  <Window x:Class="WpfApplicationComparingBackground_v_Task.MainWindow"
   2:    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   3:    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   4:    Title="MainWindow"
   5:    Height="350"
   6:    Width="525">
   7:    <StackPanel>
   8:      <TextBlock x:Name="Text1" />
   9:      <TextBlock x:Name="Text2" />
  10:      <TextBlock x:Name="Text3" />
  11:    </StackPanel>
  12:  </Window>

And this code behind:

   1:  using System;
   2:  using System.ComponentModel;
   3:  using System.Threading;
   4:  using System.Threading.Tasks;
   5:  using System.Windows;
   6:   
   7:  namespace WpfApplicationComparingBackground_v_Task
   8:  {
   9:      public partial class MainWindow : Window
  10:      {
  11:          public MainWindow()
  12:          {
  13:              InitializeComponent();
  14:   
  15:              Loaded += MainWindow_Loaded;
  16:          }
  17:   
  18:          void MainWindow_Loaded(object sender, RoutedEventArgs e)
  19:          {
  20:              BackgroundWorkerExample();
  21:              TaskContinueWithExample();
  22:              AsyncCtpExample();
  23:          }
  24:   
  25:          private void BackgroundWorkerExample()
  26:          {
  27:              var bWorker = new BackgroundWorker();
  28:   
  29:              bWorker.DoWork += (sender, args) =>
  30:              {
  31:                  // Mimic Some Long Running work
  32:                  Thread.Sleep(TimeSpan.FromSeconds(5));
  33:                  args.Result = "Using BackgroundWorker";
  34:              };
  35:   
  36:              bWorker.RunWorkerCompleted += (sender, args) =>
  37:              {
  38:                  Text1.Text = args.Result.ToString();
  39:              };
  40:   
  41:              bWorker.RunWorkerAsync();
  42:          }
  43:   
  44:          private void TaskContinueWithExample()
  45:          {
  46:              var uiThreadTaskScheduler = TaskScheduler.FromCurrentSynchronizationContext();
  47:   
  48:              Task<string>.Factory.StartNew(() =>
  49:              {
  50:                  // Mimic Some Long Running work
  51:                  Thread.Sleep(TimeSpan.FromSeconds(5));
  52:   
  53:                  return "Using TPL";
  54:              }).ContinueWith(task =>
  55:              {
  56:                  Text2.Text = task.Result;
  57:              }, uiThreadTaskScheduler);
  58:          }
  59:   
  60:          private async void AsyncCtpExample()
  61:          {
  62:              var response = await Task<string>.Factory.StartNew(() =>
  63:              {
  64:                  // Mimic Some Long Running work
  65:                  Thread.Sleep(TimeSpan.FromSeconds(5));
  66:   
  67:                  return "Using Async CTP";
  68:              });
  69:   
  70:              Text3.Text = response;
  71:          }
  72:      }
  73:  }

Although the TaskContinueWithExample() isn’t much less code than the BackgroundWorkerExample(), it just feels better to me and reads top to bottom which I think is a bit easier to grok.  Of course, the AsyncCtp() example is the least amount of and most readable code.  These three methods accomplish the same goal, but isn’t it nice that the framework / language abstractions have evolved to the simplicity / beauty of AsyncCtpExample()?

ASP.NET Web Forms, MVC, WebMatrix … what’s the story?

The reality of choices ASP.NET developers have today can be daunting:

  • ASP.NET Web Forms
  • ASP.NET MVC
    • Web Forms view engine vs. Razor view engine
  • WebMatrix / Web Pages (.cshtml / razor)

With all the noise around ASP.NET MVC and WebMatrix lately, there’s been an unfortunate side effect that creates a perception that Web Forms is passé and not getting investment from the ASP.NET team.  I’m still very surprised when I talk to ASP.NET Web Forms developers who don’t know about what’s new in ASP.NET 4.0 for them.  I think it has to do with some of the overshadowing / attention from other two, newer, choices.  The podcast below is really worth listening to if you are still scratching your head about Web Forms as it relates to these newer options. 

http://www.misfitgeek.com/2011/07/podcast-damian-edwards-aspnet-webforms/

“Damain is a Program Manager Microsoft’s ASP.NET team and is responsible for WebForms and the WebForms Developer Experience

In this episode I talk with Damain about the future of WebForms and its evolution, the influence of DynamicData on WebForms, code generation for ASP.NET WebForms and more.”

In addition to talking about futures, they also spend some time in the beginning talking about ASP.NET 4.0 Web Forms  and some other interesting topics.  They really do a good job of addressing some of the FUD around Web Forms as well as the pros/cons of our various ASP.NET based choices.  Here are a couple of my favorite quotes in the beginning:

We are focusing on making more developers happy.  Not every developer wants the same thing out of a web development programming framework (paraphrase)

Each of the frameworks has its strengths and weaknesses.  Each has its own independent model for approach how to go about doing web development.  Some people like one model over the other. (paraphrase)

Yes, there are some cooler / newer things to Web Forms, but there are also some real advantages to working in a technology that has the benefit of a decade of evolution. (paraphrase)

There are all sorts of good, rational comments about Web Forms vs. MVC vs. WebMatrix.  Whether we like the choices or not, there are real reasons for having them.  Even if you disagree with some of the comments, I think they are worth listening to and reflecting upon.  It’s an hour long podcast, but worth listening to in the background as you are working on something else.  The podcast really does put Web Forms into perspective, reminds people that the majority of ASP.NET developers are still Web Forms developers, and then helps us understand what’s coming in the future.

Don’t get me wrong.  I am a HUGE ASP.NET MVC fan, but that doesn’t mean that it is always the right choice.

I made it on the MSDN Cloud home page :)

I recently recorded an overview video called Developing Cloud Applications with Windows Azure to replace the current one on the Cloud section of MSDN.  I just heard news that the video is live.  I am pretty excited because this is THE VIDEO you see when you go to http://msdn.com, then click the Cloud section. There’s nothing deep in the video, but the goal was to comprehensively answer the “What is the Windows Azure Platform” question at enough depth that developers new to the Windows Azure Platform wouldn’t feel like they were watching too much of a marketing video.

Follow devkeydet on Twitter

PubSec Dev Dinner on External Data and Services with SharePoint 2010

My old team is having a developer dinner tomorrow night titled Developing SharePoint 2010 Solutions Using External Data and Services.  See here for more details at http://bit.ly/kPUq5n.

“…

What you will learn

SharePoint 2010 allows developers to work with both internal and external data using Business Connectivity Services (BCS), Excel Services, Access Services and custom WCF services. During the presentation we will discuss and demonstrate several common usage scenarios.

  • Bringing SQL Server data to SharePoint using BCS
  • Sharing Excel Data using PowerPivot for SharePoint
  • Publishing Access Database applications to SharePoint
  • Sharing secure data using BCS and the secure store service
  • Creating Silverlight Using WCF RIA services for SharePoint

…”

Are you using Visual Studio PerfWatson?

You can do your part to make future releases of Visual Studio faster by installing this extension:

“Would you like your performance issues to be reported automatically? Well now you can, with PerfWatson extension! Install this extension and assist the Visual Studio team in providing a faster future IDE for you…”

http://bit.ly/vsperfwat

I have it installed and haven’t noticed it getting in the way.

SCREENCAST: Hybrid Solutions with Windows Azure Connect

I’ve had many conversations lately with people about using Windows Azure to build “Hybrid Solutions.”  The typical response I get is “what’s that?”  When I explain that it is the concept of running part of your application in Windows Azure and part of it in your own datacenter, the typical response I get is “I didn’t know that was possible.”  Not only is it possible, but it is relatively painless to setup.  Two of the common scenarios that this approach applies to are:

  • Enterprise app migrated to Windows Azure that requires access to on-premise SQL Server
  • Windows Azure app domain-joined to corporate Active Directory

A good example of the first scenario is the need to protect data “at rest” in order to comply with laws, regulations, and guidelines established in various industries.  SQL Azure does not currently offer the Transparent Data Encryption (TDE) feature like its on-premises sibling (SQL Server).  This is a perfect scenario for a hybrid solution using Windows Azure Connect.  In this scenario, you still get the opportunity to take advantage of many of the cloud computing benefits the Windows Azure Platform such as Compute (Web/Worker/Virtual Machine) Roles, Caching and more

So in an effort to do my part to help raise broader awareness, I recorded this screencast:

http://bit.ly/azureconnecthybrid

Help make Microsoft developer technologies better!

Follow devkeydet on Twitter

Ron Jacobs just blogged about how .NET developers can provide feature feedback and vote on WCF/WF features.

http://blogs.msdn.com/b/rjacobs/archive/2011/04/14/how-you-can-make-wf-wcf-better.aspx

Many Microsoft product teams are doing this nowadays. It still surprises me how many .NET developers don’t realize these feature voting sites exist. In addition to WF/WCF, I am aware of these:

http://wpdev.uservoice.com/forums/110705-app-platform

https://windowsphone7community.uservoice.com/forums/84435-feature-feedback

http://data.uservoice.com/forums/72027-wcf-data-services-feature-suggestions

http://data.uservoice.com/forums/72025-ado-net-entity-framework-ef-feature-suggestions

http://dotnet.uservoice.com/forums/40583-wpf-feature-suggestions

http://dotnet.uservoice.com/forums/4325-silverlight-feature-suggestions

http://dotnet.uservoice.com/forums/87171-visual-basic-content-requests

http://dotnet.uservoice.com/forums/57026-wcf-ria-services

http://www.mygreatwindowsazureidea.com/pages/34192-windows-azure-feature-voting

http://www.mygreatwindowsazureidea.com/forums/35889-microsoft-codename-dallas-feature-voting

http://www.mygreatwindowsazureidea.com/forums/44459-sql-azure-data-sync-feature-voting

http://www.mygreatwindowsazureidea.com/forums/34685-sql-azure-feature-voting

http://www.mygreatwindowsazureidea.com/forums/100417-sql-azure-reporting-feature-voting

http://www.mygreatwindowsazureidea.com/forums/40626-windows-azure-appfabric-feature-voting

http://www.mygreatwindowsazureidea.com/forums/103009-windows-azure-code-samples-voting

http://www.mygreatwindowsazureidea.com/forums/103403-windows-azure-content-voting

http://aspnet.uservoice.com/forums/41199-general

http://aspnet.uservoice.com/forums/41201-asp-net-mvc

http://aspnet.uservoice.com/forums/41202-asp-net-webforms

http://aspnet.uservoice.com/forums/50615-orchard

http://aspnet.uservoice.com/forums/100405-performance

http://aspnet.uservoice.com/forums/41233-visual-studio-performance-feedback

Let me know in the comments if I’ve missed any.  I’ll add them.

SCREENCAST: JavaScript Intellisense for SharePoint

Follow devkeydet on Twitter

In this screencast, you will learn how to get the most out of JavaScript programming with SharePoint 2010 projects in Visual Studio 2010. You will see how to get JavaScript Intellisense and debugging working for jQuery and the Client Object Model. You’ll also learn about the benefits of using the Microsoft Ajax Content Delivery Network (CDN).

If you don’t pause your video player at the right time, you might miss the location of the SharePoint JavaScript files. Here is a link to the MSDN How to article that talks about those files:

http://msdn.microsoft.com/en-us/library/ff798328.aspx

Direct link:

https://channel9.msdn.com/posts/JavaScript-Intellisense-for-SharePoint

OQuery – A fluent API to build OData url queries sans LINQ

Yesterday, I blogged about a solution to compose OData / WCF Data Service queries using LINQ for situations where LINQ enabled client libraries don’t exist (i.e. JavaScript and Windows Phone 7).

http://blogs.msdn.com/b/devkeydet/archive/2011/02/02/data-services-odata-client-for-windows-phone-7-and-linq.aspx

The post is all about using LINQPad as a tool to write your LINQ queries, then using the feature in LINQPad that gives you the url query translation.  Well today, a new MSDN Code Callery project just popped up called OQuery that offers another approach that doesn’t require using an external tool such as LINQPad:

http://code.msdn.microsoft.com/oquery

Here’s the description from the project page:

“OQuery is a library which gives you a fluent style interface for building OData Url Fragments in javascript or C#.
Neither Javascript or Silverlight for WP7 support LINQ and so this library in those cases.”

What are you waiting for?  Go check it out!

Follow devkeydet on Twitter