This is just a picture down there. The last time I got this exception, it was a missing exit condition on a recursive loop in a Java program that a friend of mine was writing. I have got one from C# compiler, while I tried build a Console project couple of years ago. Restarting Visual studio fixed that one. Never really had a chance to take a screenshot, cause I think these are things that you don’t encounter often unless you explicitly tried for a demonstration. So here it is, archived.
This article was originally published for www.prowareness.com and could be located at http://www.prowareness.com/blog/show-ies-file-new-session-some-love/
Guess what? You do not need multiple browsers an IE, a Firefox, a Chrome, when you want to open three different yahoo/facebook/gmail or any different sessions. Internet Explorer starting Internet Explorer 8 supports New Session feature out of the box, whereas in other browsers you would need to pimp for third party plugins/addins to achieve the same thing.
Somehow this feature was not marketed properly to the developer community and I guess the reason was the surge in Chrome’s market share – when IE 8 was released back in 2008, Chrome was a ruthless rookie to the browser wars boasting speed by all means, and Internet Explorer started to decline.
Name it InPrivate browsing, Incognito, Private Browsing – they all do one common thing. Forget the existence of anything that existed before the start of a browsing session ( and forget the existence of anything that existed in particular the browsing session, after it has ended ).
Here, the word session could relate to the session state phenomena (with cookies), sometimes browser history, and temp files and all other flossy stuff that browser manufactures use to track this and that. So there you have it, you could have multiple sessions via the concept of private browsing.
When you have IE, if you do not want in private browsing, all you have to do is go to File menu and hit New session
From a developers and testers perspective, this is a silver bullet solution with just one browser – Internet Explorer, if faced with a scenario where a website uses session state (which is almost all the time), and you would want to open multiple users at the same time.
Make a shortcut on your desktop, start menu, pin it with the Command line:
Put a –private next to it if you want to open in IE InPrivate browsing.
This article was originally published for www.prowareness.com and could be located at http://www.prowareness.com/blog/tpl-and-thats-why-its-called-cooperative-cancellation/
It’s a practice to provide a Cancel button to a ‘tired of waiting’ user for a long running operation. In WinForms, you would probably create a BackgroundWorker or a separate thread to keep the UI responsive to the user can click the Cancel button. When the user clicks the Cancel button, how you actually implement the cancellation logic is the topic of this blog.
Please read Interrupt Politely, to get a complete overview of the cancellation options. Now, let’s get in to the types introduced with .Net 4.0 and what TPL has to offer.
The CalculateSum method below is our long running operation, that does nothing but adds a couple of numbers; keeps us posted on the calculated value via Console.WriteLine and sleeps on a condition.
The method CallerWithoutCancellation calls the above CalculateSum method via a Task.
Imagine, if the user wanted to cancel this long running task, there is no provision for cancellation that the method CalculateSum provides. If you ran CalculateSum with a classic Thread as opposed to a Task, then you would either issue a Thread.Abort or Thread.Interrupt. This is a decision you have to make carefully. Option 1 in the drdobbs journal and Aborting and Interrupting Threads talk more about them.
But with Task Parallel Library, CancellationTokenSource and CancellationToken, we have better options to cancel.
Let’s look at the implementation of the CalculateSum method with a new overload taking a CancellationToken parameter named inputCancellationToken. Pay careful attention to the line inputCancellationToken.ThrowIfCancellationRequested().
The caller, CallerWithCancellationToken, has a cancellation option on press of the Enter key; then it calls cancellationTokenSource.Cancel(). Also pay attention to the second parameter cancellationToken in the Task.Factory.StartNew method.
The CalculateSum method provides a provision for cancellation using the CancellationToken and the CallerWithCancellation method requests for a cancellation via the same CancellationToken.
As an alternative, you could also poll on the IsCancellationRequested property and proceed with clean up and cancellation.
What you effectively have here is a mechanism where the caller signals for a cancellation request via cancellationTokenSource.Cancel(), and the callee responds to it by cancelling itself via inputCancellationToken.ThrowIfCancellationRequested() or performing necessary clean up operations by checking inputCancellationToken.IsCancellationRequested.
Now, you see a coordination, an agreement between the caller and the callee, or in other words, they both cooperated for cancellation. And that’s why it’s called Cooperative Cancellation.
Download and play the source code here.
Note: This pattern existed way before TPL, but you had to rely on synchronization mechanism when reading or writing the cancellation flag. Now with TPL, it is build in with the CancellationTokenSource and CancellationToken.
This article was originally published for www.prowareness.com and could be located at http://www.prowareness.com/blog/writing-helper-like-inline-helper-method-for-aspx-view-engine/
When I started my MVC course with ASP.NET MVC 3, I got introduced to the @helper syntax with the Razor view engine. In case you didn’t know what an @helper syntax is, you may read more about it here. It is common that during training sessions, certain features of a technology may not appeal to you until you really use it, or until you have someone who puts that in perspective for you. So, I was dealing with an MVC application with ASPX view engine and I had a situation where I thought if I had Razor view engine, I could use the @helper syntax, and here’s what I ended up with.
The @helper syntax within Razor enables you to easily create re-usable helper methods that can encapsulate output functionality within your view templates. They enable better code reuse, and can also facilitate more readable code. – Source
In this post, I will show you can write a Razor style @helper method for ASPX view engine. Some people like to call it the Inline helper method If you look at the end result of the helper method in Razor view engine and ASPX view engine, it not aesthetically similar or lucid, but it solves the purpose – you get re-usability within the view. There are alternative ways of achieving the same thing using a Html helper of a server method within the view, but I ended with this approach.
In the ASPX view engine, if you declare and initialize a variable, if would show up in the intelli-sense. In the same way, if you try declaring an Action delegate, it would show up in the intelli-sense.
And that’s the trick. Declare an Action delegate, initialize with an anonymous delegate. You are done.
Note that when you initialize the Action delegate, you could either use the explicit delegate keyword or just a lambda.
Download: Sample projects including the source code for @helper sample in Razor view engine, and the Action delegate way of doing it in the ASPX view engine is downloadable at the SkyDrive location – http://sdrv.ms/15ykUXD
This article was originally published for www.prowareness.com and could be located at http://www.prowareness.com/blog/di-unity-mvc3-and-object-reference-not-set-to-an-instance-of-an-object/
When you use Unity.Mvc3 or Unity.Mvc4 for DI, odds are you might not even run in to the below exception/stack trace.
But you’d never know what kind of system you are dealing with. By name, these dlls – Unity.Mvc3, and Unity.Mvc4 – imply that they need to be used with MVC applications; which means, they are many things related to a web page or web request that this dll might possibly expect, for instance, the web context, the authorization context, routing context and so on.
We were dealing with a system where we tried using Unity.Mvc3 for dependency injection, and a very few parts of existing functionalities started failing with the below stack trace.
This exception occurred because we invoke a background worker from the UI (to run a long running Task), and when the background worker finishes, it would call in RenderViewToString, and update the UI. During the second part a System.NullReferenceException was thrown. With the given stack trace it was evident that this was happening due to the Unity.Mvc3 at Unity.Mvc3.UnityDependencyResolver.get_ChildContainer().
Searching internet yielded no results. We went and looked at the Unity.Mvc3 source code at and found that property named ChildContainer was expecting a HttpContext and our BackgroundWorker didn’t have that for obvious reasons. Hence the Object reference not set to an instance of an object.
Credits to Brijesh, where he had it spot on when he saw the word HttpContext. We ended up removing Unity.Mvc3 completely and used the infamous custom dependency resolver implementing the System.Web.Mvc.IDependencyResolver interface.
So security is two different things: it’s a feeling, and it’s a reality. And they’re different. You could feel secure even if you’re not. And you can be secure even if you don’t feel it. Really, we have two separate concepts mapped onto the same word. …
…Every species does it. Imagine a rabbit in a field, eating grass, and the rabbit’s going to see a fox. That rabbit will make a security trade-off: "Should I stay, or should I flee?" And if you think about it, the rabbits that are good at making that trade-off will tend to live and reproduce, and the rabbits that are bad at it will get eaten or starve. So you’d think that us, as a successful species on the planet — you, me, everybody — would be really good at making these trade-offs. Yet it seems, again and again, that we’re hopelessly bad at it. And I think that’s a fundamentally interesting question. …
…We tend to exaggerate spectacular and rare risks and downplay common risks — so flying versus driving. The unknown is perceived to be riskier than the familiar. One example would be, people fear kidnapping by strangers when the data supports kidnapping by relatives is much more common. This is for children. Third, personified risks are perceived to be greater than anonymous risks — so Bin Laden is scarier because he has a name. And the fourth is people underestimate risks in situations they do control and overestimate them in situations they don’t control. So once you take up skydiving or smoking, you downplay the risks. If a risk is thrust upon you — terrorism was a good example — you’ll overplay it because you don’t feel like it’s in your control….
I was busy writing the TranslateMaid Addin for Visual Studio and learned so many things in so many ways. One of the many quirks I noticed with the Microsoft Translator Preview service was, when you send an XML element or simply put some text or character enclosed with < and > and some text or character following it, you get a freebie. And what’s that?
For instance, when you send <summary>a as the sourceText parameter to be translated, then the result you yield is <summary>a</summary>. And that’s the freebie.
If this was an expected behaviour, then there is a mistake. If this was a mistake, then there is a mistake in that mistake! I will explain why.
You try with <summary>a, <seealso>a, <returns>a, <Jey>a, <maran>a, <a>a, <sometext>a and for almost everything it returns a free end tag. But when you pass <param>a or <input>a, you don’t get a freebie. They may be many more such examples.
In the below samples, %3C and %3E denote < and > in when URL encoded.
and you don’t get the freebie for
Working on a foreign language Code base in Visual Studio? Try TranslateMaid for Visual Studio.
http://translatemaid.codeplex.com/ – Download, install with a few clicks, see translated text right within Visual Studio.
With the release of Internet Explorer 8, Microsoft introduced Accelerators, and one of the accelerators were ‘Translate with Bing’. TranslateMaid.TryTranslate uses the same Microsoft Translator Preview Service.
I don’t know why I like IE, but sometimes it is just so hard to break a habit. While Chrome offers you a feature to translate the entire webpage, IE also allows you to translate just a selected text. I was thinking about bringing the same interface inside Visual Studio, and I pretty much achieved it. I had a tough time switching between a translator and visual studio understanding a lot of Dutch comments.
If you have some comments in your project files, I hope now you could save a couple of seconds switching between Google translate and Visual Studio.
1. Source Language is Auto Detected.
2. After the Translator window opens, you could convert the translated text to a variety of other languages.
3. Adds buttons to the Tools menu and Code Window’s context menu.
4. Keyboard Shortcuts enabled Ctrl+`, and more. Full documentation here.
Try it. Pass it on. If you are curious about how it’s done, feel free to look through the source code at CodePlex. Tested thoroughly with VS 2012, VS2010, VS 2008 (inclusive of Business Intelligence Development Studio) on Windows 7 x64, and Windows Vista x86, but guaranteed to work with other versions of Windows as well.
If there is another version of this add-in, it might support an Options dialog with transparency control when the translation is shown, choice to switch between Google and Microsoft Translator Service. If you stumble on any bugs, bring to my attention.
What did it take to complete?
1. A Fiddler trace while IE is running that yielded me http://www.microsofttranslator.com/DefaultPrev.aspx?ref=IE8Activity
2. Visual Studio’s Addin template for the starters.
3. Classic WinForms tricks, and the interesting Mouse Hook in C#.
4. Extensive articles, code samples from MZTools.com
5. and some long nights..
Spread the joy Develop some apps, Make the world a better place
This article was originally published for www.prowareness.com and could be located at http://www.prowareness.com/blog/run-as-administrator-for-sln-files-and-other-context-menu-essentials/
Download the file named RunAsEssentials-RegistryKeys.zip from the SkyDrive location http://sdrv.ms/1ds9pbZ.
Many a times you would want to run certain tasks as Administrator (yeah, to run a program as elevated). As a developer, you might typically find yourself doing it for Visual Studio Solution Files (.sln). I have been using a set of hacks to enable ’Run as administrator’ for .sln files, and to open any file with Notepad, or with Notepad as administrator and a couple more of them.
Below are some screenshots of what you would get when you download the zip file named RunAsEssentials-RegistryKeys.zip and run required .reg files or the .bat file from the aforementioned SkyDrive download location.
If you’d like to do it for any other file extension, let’s say for .csproj files, then locate the .csproj file extension under the HKCR registry hive, look for the default value of the .csproj key. If it is VisualStudio.Launcher.csproj.11.0 then navigate to that key location. Crack open the command subkey key under Open . Copy the default value "C:\Program Files (x86)\Common Files\Microsoft Shared\MSEnv\VSLauncher.exe" "%1".
Create a new key named runas and a subkey under it named Command . And associate the Command key’s default value as "C:\Program Files (x86)\Common Files\Microsoft Shared\MSEnv\VSLauncher.exe" "%1".
To add a Shield icon to the context menu option add a value named HasLUAShield to the runas key. LUA expands as Limited User Account.
Adding keys to the HKCR makes changes to all the users in the computer. If you’d like to do it only for a particular user, then find and edit the file extension handlers under HKCR\Software\Classes.
If you get Access Denied or Merge failed or Cannot Import Error accessing the registry error messages, then open a command prompt as administrator and try running the .bat or .reg files.
Although it is said/recommended to take a back up of your registry before doing any changes, these registry files possess no harm per se. All these .reg files are provided in good faith and with goodwill, so use it at will.