background preloader

CodeProject

Facebook Twitter

Task Parallel Library : 2 of n. Download demo code - 132 KB Introduction This is the second part of my proposed series of articles on TPL. Last time I introduced Tasks, and covered this ground: Thread vs. Tasks Creating Tasks Trigger Methods/Properties Handling Exceptions Cancelling Tasks SynchronizationContext This time we are going to be looking at how to use a TPL concept called Continuations.

Article Series Roadmap This is article 2 of a possible 6, which I hope people will like. I am aware that some folk will simply read this article and state that it is similar to what is currently available on MSDN, and I in part agree with that; however, there are several reasons I have chosen to still take on the task of writing up these articles, which are as follows: All that said, if people having read this article truly think this is too similar to MSDN (which I still hope it won't be), let me know that as well, and I will try and adjust the upcoming articles to make amends.

Table of Contents Some more TPL background. Some Useful Features of C# Introduction In this article, I am going to discuss some important facts which might be unknown to most developers. I am going to discuss about three different topics which are related to C# and are more helpful when building your application. Why use StringBuilder over String to get better performance Structure initialization in C# Checked operator Go To with Switch...

Case I am going to discuss the above list one by one. Why use StringBuilder over String to get better performance There are a number of articles and posts that say that StringBuilder is more efficient because it contains a mutable string buffer. .NET Strings are immutable, which is the reason why a new String object is created every time we alter it (insert, append, remove, etc.). In the following section, I am going to explain this in more detail to give beginners a clear view about this fact. I wrote the following code, and as you can see, I have defined a String variable and a StringBuilder variable. Summary Facts int a =0; or. FolderBrowserDialogEx: A C# customization of FolderBrowserDialog. Download source code - 21.1 KB Introduction We've all used FolderBrowserDialog and its precursor SHBrowseForFolder.

While these common controls are very useful in stock form, they never quite seem to do exactly what you want - there's always some UI artifact that you really don't want. And, like many things in .NET, the managed API seems to be a thin wrapper around the unmanaged API, and the managed API therefore inherits all the quirks.

I had a few gripes about FolderBrowserDialog, especially: The stock "Browse For Folder" dialog title... Background In setting out to solve the issues, I started in the usual way: search the Web for code samples! Neither of which was the exact solution I sought, but both provided invaluable starting points. Using the Code XBrowseForFolder does a great job explaining the techniques for using SHBrowseForFolder, so I won't rehash that here. The attached sample code contains a FolderBrowserDialogEx classlib and a WinForms app that demos it. Points of Interest. A Silverlight Sample Built with Self-Tracking Entities and WCF Services - Part 3.

Please visit this project site for the latest releases and source code. Article Series This article is the third part of a series on developing a Silverlight business application using Self-tracking Entities, WCF Services, WIF, MVVM Light Toolkit, MEF, and T4 Templates. Contents Introduction In this third part, we will focus on data validation with self-tracking entities. The purpose of using data validation is to make sure that any data is validated before being stored in the database.

How to Add a Validation Attribute WCF RIA Services has built-in support for member/field level validation, entity level validation, and operation level validation. For the EF auto-generated entity classes, we can add validation attributes on the properties of the metadata class like follows: [MetadataTypeAttribute(typeof(User.UserMetadata))] public partial class User { internal class UserMetadata { protected UserMetadata() { } ......

Adding Client-side Only Validation The new password is a required property. Prism for Silverlight/MEF in Easy Samples. Part 3 - Communication between the Modules. Download samples - Part 3 - 278.7 KB Introduction This is the 3rd (and last) part of "Prism for Silverlight/MEF in Easy Samples" Trilogy. It describes communications between different modules within the application. Here are the pointers to Part 1: Prism Modules and Part 2: Prism Navigation. This part of the tutorial assumes some knowledge of C#, MEF and Silverlight as well as the concepts of Prism modules and regions which can be learned from parts 1 and 2 of this tutorial. As we learned in Part 1: Prism Modules, Prism modules are independently deployable software units (.dll files in WPF and .xap files in Silverlight).

The main module which is used to assemble other modules is called "application". Communication between different modules is a little bit of a challenge since most modules do not reference each other (the independence condition) and thus cannot access each other's functionality directly. There are three ways for Prism modules to communicate between each other: Acknowledgment. Data and Command Bindings for Silverlight MVVM Applications. Download source - 40.59 KB Introduction This article summarizes two utility classes used for data and command bindings in Silverlight MVVM applications and demonstrates how to use the two classes with a running example. Background MVVM design pattern is now the default design pattern for building Silverlight applications. Data binding and Command binding are the fundamental building blocks when developing MVVM applications in Silverlight.

This article will summarize two very commonly used utility classes to help implement bindings in Silverlight MVVM applications and presents a running example to use these two classes. The "ViewModelBase" class is to help in implementing data binding. If you are interested in more general discussions related to MVVM in Silverlight, you can easily find references, such as this one, this one and this one. The attached Visual Studio solution has two projects: The "MVVMUtility" project is the Silverlight application. The Data Model The View Model The View History. Resetting a View Model in WPF MVVM applications without code-behind in the view. Download demo source code - 15 KB Introduction The core idea behind this code and article is to provide an uncomplicated approach to reset a view-model without having to adjust DataContext references in views.

It all started with an MSDN forum thread where someone asked if there was an easy way to clear a view model. He felt that clearing every property in the view model would be too cumbersome and that it would be simpler if he could have a Cancel button that would reset the DataContext to a new view model instance. The negative (so to speak) would be that he would have to have actual code in his view (the handler for the Cancel button) that did this. I kept two fundamental requirements in mind when I designed this class : The developer should be able to reset the view model without any code in his view.The class should be transparent, so the user should not have to change his existing view model in any way to use this class. Why use this class at all? What my class achieves is this:

Task Parallel Library : 1 of n. Introduction I recall the first time I created a UI in .NET that had to go and get some data from a database, and the amount of data that I fetched was way larger in production that my code assumed it would be in my dumbed down test setup. Guess what happened... my UI froze as soon as it used real data. The reason for this is that the UI thread (i.e., the only thread in my naive UI) was being used to carry out this fetching of data from the backend database. Yikes! As it turned out, there was. .NET offered (and still does) a wide variety of Threading classes to help with just this sort of situation, such as Thread, BackgroundWorker, ThreadPool etc. So I got to know and love some of these classes in the System.Threading namespace, and they did make a massive difference to the responsiveness of my application, all cool.

Luckily, help is at hand. A TPL Task actually uses the ThreadPool internally, if you use the default scheduler, which you more than likely will most of the time. Animated WPF Panels. Introduction This article discusses how to apply custom animation to an already existing WPF layout by extending classes such as Grid, StackPanel, DockPanel or WrapPanel. Or indeed any other WPF control that hosts other UIElements. An important part of this implementation is that is should be able to extend any existing panel and cope with any child controls of that panel. By animate in this context, I'm referring to the process of having all the components of a panel move to their positions and change their size over a time.

This is a VB.NET article so all the snippets will be in VB.NET, but for completeness I've included both a C# and a VB.NET solution with roughly equivalent implementations. Sample Video I've posted a YouTube video of this project, check it out, the wrapping text thingy looks really cool. Background I find that there are two easy ways of making a UI more visually appealing: Animate stuff Do stuff in 3D Using the Code The Approach It looks something like this: History. How to implement subcription based on WCF. Download WCFSubscription - 119 KB Introduction The first question is what is Synchronized Distributed System? I'm not sure of the official term. I named it from my understanding. The typical application is stock trade software. In this part, we are going to simulate a similar scenario: Inventory Monitor.

Background My final purpose is to design a service bus mechanism based on WCF. Using the Code It is actually not difficult to make a remote call. [ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(IMessageSenderCallBack))] public interface IMessageSender { [OperationContract(IsOneWay = false, IsInitiating = true, IsTerminating = false)] void Init(); } [ServiceContract] public interface IMessageSenderCallBack { [OperationContract(IsOneWay = true)] void SendMessage(Message message); [OperationContract(IsOneWay = true)] void SendHeartBeat(); } The subscription in WCF is a little bit like what we called observer design pattern.

SessionMode = SessionMode.Required. Code Contracts in .NET 4.0 & its Internals. Download CodeContract sample - 44 KB After I have introduced the Internals of Exception handling, I mentioned that I will cover some of the features that are coming as an improvement to it. Code Contract in .NET 4.0 is one among them. In this post, I will cover how you could use code contract in your application and also take the way through to its internals later on. One can argue with me that code contract is a part of unit testing and might be included as a part of .NET test projects, but according to me, it is not restricted only for testing frameworks, you could use it more likely for your own libraries so that you could have better code handling and also some way of separation between the validation logic for which the library is built in and how you would react with the outside world.

Now let us discuss how to use it in your program, and later on I will build an application to use it. The Basis This looks horrible, right. Installation Precondition Hmm, doesn’t it look good on me?