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.