Wednesday, June 4, 2014

First CTP of Visual Studio "14"

Yeah you heard it correct, Visual Studio "14" is on the way and most likely it will be available by 2015. This will include few major updates of Roslyn with better refactoring support, new ASP.Net templates for vNext, new features of C++ 11 and much more. List of known issues with Visual Studio "14" and download details can be found here.

So, get ready for something new again :)

Thursday, May 29, 2014

Propagating Property Change for Static Properties

While working on any XAML based app, first thing which comes into mind is Binding. There are lot many articles on what is Binding and how it works. Don’t worry, I am not going to repeat all that stuff again. But definitely, I would like to touch upon few things which are base of my today’s write-up.

To make any property bindable or let’s say to propagate property change, we usually follow one of the below two options:
  • Implement INotifyPropertyChanged interface or
  • Create an event with name PropertyNameChanged

Point to notice here is, both the above options will work only on instance properties. Now what if my property is Static???

INotifyPropertyChanged is not going to work for static properties. None of the above options will make x:Static extension work.

What to do now ?

No worries, all these hazards can easily be overcome when you will jump to .Net 4.5.

Approach 1: Property specific static event for each and every static property
Let’s have a look at the below code:

Analysing Approach 1: Given approach will be feasible only when we have 1 or 2 static properties. If there are many static properties, then code will be bulky and un-maintainable as each property will have separate event.

Solution: Instead of writing static event for each and every static property, one generic event should be there.

Approach 2: Single Generic static event for all the static properties
Let’s have a look at the below code:

Analysing Approach 2: This approach will work very smoothly irrespective of how many static properties are present in a given class.

Point worth mentioning about EventHandler parameters:

Why null is passed as a parameter in EventHandler?
First placeholder is for passing instance. But as this is Static property, instance value will be null. 

Hope you enjoyed learning this new feature.

Tuesday, May 27, 2014

Identify WPF version

If you want to figure out the current version of WPF installed on your machine, then one has to navigate to registry for below path:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.0\Setup\Windows Presentation Foundation

Version value on above path will give your the required information.

Monday, May 12, 2014

.Net Framework 4.5.2 is available

Microsoft recently released .Net Fx 4.5.2 which is in-place update for .Net Framework 4, .Net Framework 4.5 and .Net Framework 4.5.1 and is ready for download on below links:

This includes updates for ASP.Net, Windows Event Tracing (ETW), better profiling as well as for DPI settings. Enjoy...

Thursday, April 24, 2014

EF warning while using Database first approach

Error 6002: The table/view 'AdventureWorks.dbo.BuildVersion' does not have a primary key defined. The key has been inferred and the definition was created as a read-only table/view.

Explanation: The above warning is generated when you use Database first approach for Entity Framework model creation. Such warning appears due to entity key. Every Entity needs to have an entity key and when EF creates an entities, what it does is, it tries to find an primary key from the database and creates an entity key from that. 

But it may be the case that few of the database objects doesn't have primary key defined. In that case, EF tries to infer a key based on your view or table's columns. So, here it becomes necessary to have at lease a non-null column in your database object. 

It's just a warning, so nothing harmful here. But still if you need a solution to get rid of this warning, then it is posted by Hilmi Aric.

Monday, April 21, 2014

Visual Studio Extension for Installer Templates

Finally Installer Projects templates are back with Visual Studio. Hope most of you are aware that few years back, Microsoft removed the templates for installer projects from Visual Studio and we all were obliged to use InstallShield. 

Till Visual Studio 2010, these templates were shipped along with VS but post releases were not having these templates. But user voice and vocal feedbacks made this possible and now these templates are again part of Visual Studio 2013 and will continue to be the part in all the Visual studio’s future versions.

This Extension can be downloaded either from Visual StudioGallery or by using ‘Extensions and Updates’ dialog’s online section with a search string as ‘Visual Studio Installer Projects Extension’.

Important point to mention here is, Visual Studio will still provide the in-box solution for InstallShield limited edition. As of now, this update is part of preview release version;)

Thursday, April 17, 2014

Deadlock and Livelock

Deadlock: Deadlock is a situation when two processes, each having a lock on one resource and attempt to acquire a lock on the other's resource. In this case, each process would have to wait indefinitely until one of them releases the lock on the holding resource.
Livelock: Livelock is a situation, where in a request for an exclusive lock is repeatedly denied because of series of overlapping shared locks keeps interfering. A livelock also occurs when read transactions monopolize a resource(file/page/memory location), forcing a write transaction to wait indefinitely.

Wednesday, April 9, 2014

Controlling Degree of Concurrent Execution in Parallel Loops

Having multi-core machine, one can get the benefit of Parallelism for long-running and blocking tasks. I hope most of you might have used Parallel. For, Parallel. Foreach and TPL several times. Generally parallelism is used to get the benefits of all the available cores on the machine that commonly leads to the shortest execution time for any task.
  1. Did you ever thought about what can be the possible demerits of using all the available cores for just a single application?
  2. Is it really required to use all the available cores of your machine for a single application? Can't we use only a few of the cores?
  3. Is there any way to restrict these parallel loops in terms of cores?
The answer of all the preceding questions is YES.

But why should we bother about how many cores are participating in execution?
Well, there can be several reasons behind this. The foremost reason that I feel is, when a single time consuming application/task is running on all the available cores utilizing most of the processing power, what will happen to other applications running on the same machine. Those other applications might hang and may even encounter performance issues, which is not at all acceptable. Isn't it?

Another reason can be, if your long running process is executing on the server, then it is not a good idea to raise an unlimited number of requests at the same time, since it may lead to server timeout or may introduce a DoS attack. In layman words, your server may go down, which is again an unacceptable behavior. Isn't it?

All the preceding problems can be resolved by managing the available cores explicitly, especially when dealing with parallel loops. Instead of using all the cores for a single long running process, use only a few of them. So, the other cores can be used by the rest of the applications.

Before proceeding, let's have a look at a small code snippet.

Parallel.For(1, 10, i =>
    Debug.WriteLine("{0} is executed on Task {1}", i, Task.CurrentId);

If you run the code above multiple times, definitely your output may vary. Let's have a look at a few outputs:

Output on first run
3 is executed on Task 2
7 is executed on Task 4
4 is executed on Task 2
6 is executed on Task 2
1 is executed on Task 1
9 is executed on Task 5
8 is executed on Task 4
2 is executed on Task 2
5 is executed on Task 3

Output on second run
9 is executed on Task 5
5 is executed on Task 3
7 is executed on Task 4
3 is executed on Task 2
1 is executed on Task 1
6 is executed on Task 3
2 is executed on Task 5
8 is executed on Task 4
4 is executed on Task 2

How to control this degree of concurrency?
There is already a property available in C#. So, by using this property one can restrict the number of concurrent tasks created during the execution of parallel loops. By assigning some value to MaxDegreeOfParallelism, we can restrict the degree of this concurrency and can restrict the number of processor cores to be used by our loops. The default value of this property is -1, which means there is no restriction on concurrently running operations. Let's quickly jump to the code:

ParallelOptions po = new ParallelOptions();
po.MaxDegreeOfParallelism = 2;
Parallel.For(1, 10, po, i =>
    Debug.WriteLine("{0} is executed on Task {1}", i, Task.CurrentId);

In code above, I am setting MaxDegreeOfParallelism to 2, which means that only a maximum of 2 tasks will be created, that will in turn use fewer cores, which is 2 here. On executing the code above, you will get the following output:

Output on first run
5 is executed on Task 2
6 is executed on Task 2
7 is executed on Task 2
8 is executed on Task 2
9 is executed on Task 2
2 is executed on Task 2
3 is executed on Task 2
4 is executed on Task 2
1 is executed on Task 1

Output on second run
1 is executed on Task 2
2 is executed on Task 2
5 is executed on Task 1
6 is executed on Task 1
3 is executed on Task 2
7 is executed on Task 1
4 is executed on Task 2
9 is executed on Task 2
8 is executed on Task 1

Whatever number of times you execute your code above, the number of concurrent tasks will never go above 2.

The same concept can be applied for a parallel Foreach loop also.

When concurrency needs to be controlled ?
It is not necessary that you always need to tweak this setting. It fully varies from scenario to scenario. Please use it with caution. The most common scenarios for using this setting is:
  1. When a huge number of automatically created threads may lead to deadlock or livelock
  2. When you want your loop or algorithm to use only a limited number of cores. It is usually required when more than one time-consuming algorithm needs to be run simultaneously
Happy learning!!!
Hope you enjoyed concurrency.

Thursday, April 3, 2014

New features of Windows Phone 8.1

2nd April 2014, during the //BUILD 2014 conference Microsoft officially showcased the set of new features which will be coming as part of Windows Phone 8.1. In my this post, I am touching those features.

1) Cortana - new personal assistant: 
Cortana is voice-based virtual assistant provided by Microsoft. It is similar to Apple’s ‘Siri’ and is powered by Bing. It can do everything that one would expect from any virtual assistant. Cortana offers advanced settings and preferences with more than the default voice commands. One can talk with her and can also give her instructions to perform any operations behalf on you. Cortana is fully based on your search queries, she learns your interests, she learns about the most important people in your life. For example, you can instruct her “Cortana, call Shweta on Skype” and it will open Skype to call the person. Isn’t it interesting ?
You can also use Cortana to set reminders by saying “Cortana, remind me to go to parlor when I am in MG Road” and she will remind you whenever you are in MG Road.

2) Sensing capability:
  • Battery Power Sense – App will monitor which all apps are drawing more power and will turn them OFF when not in use
  • Data Sense – Detailed tracking of data can be done remotely based on time(a month, a week, etc)
  • Wi-Fi Sense – Wi-Fi can be auto enabled after certain duration of time. Another interesting thing is, Wi-Fi Sense app will automatically connect to nearby available free Wi-Fi hotspot and will accept the terms of use automatically for us.

3) Action Center for notifications:
Action center will show notifications related to calls, messages, emails, etc. It will also provide quick settings to Flight Mode, Wi-Fi, Bluetooth and Rotation Lock options. Interesting thing is these quick access options are customizable.

4) Internet Explorer 11:
IE11 will be able to save and remember the passwords for websites, allows uploading of files, YouTube player including HTML5 video support and many more…

5) Enhanced Calendar:
It will allow you to synchronized Google calendar in your WP, supports week view and also supports multiple calendars which will allow you to view all the invites in one place.

6) Enhanced Email feature:
In //BUILD 2014 conference, Nick showcases on how emails can be encrypted and signed before sending to anyone. This enhancement, also allows user to download images attached or associated with email.

7) VPN Support:
WP 8.1 has in-built VPN support with ability to auto-trigger VPN connectivity. One of the very useful part of this enhancement is, it allows to password protect office documents and also allows access to corporate resources which are behind the firewall. Isn’t it cool?

8) Additional features to Camera:
Photography experience is also changed by adding few features like quick access to clicked images and tweaking tools. One good addition is burst mode features for clicking continuous images

9) More minor changes:
  • New lock screen with multiple themes
  • Improvements in Music, video apps
  • Backup of app + game data to OneDrive
  • App filtration based on install date and usage
  • Option to update store apps automatically
  • Smart search
  • Silent as well as actionable notification for apps
  • Rebranding of SkyDrive to OneDrive
  • Double tap to Unlock and Power off
  • Separate volume controls ringtones and music
  • Back button is modified to suspend app, instead of closing the app
  • Support for in-call speech commands
  • Chkdsk for testing SD cards health
  • Miracast support

Hope all of you will enjoy these new features.

Wednesday, April 2, 2014

Cursor Position on Enter key

Recently, one of my followers requested for a code snippet to implement the ENTER key press. The requirement was to move the cursor on the next WPF UI element, whenever the ENTER key is pressed.

Implementation of the above requirement is straight forward, if one has the knowledge of InputBindings and Command. Using these two, MoveFocus method of currently focused element, can be called, as shown in the mentioned below code snippet:

Hope it will help you.