and has 0 comments
Don't you hate haters? Never mind that. This kind of question is similar in logic to the CrashIE site that I've just visited (and of the malignant piece of code there - do not open the site with Internet Explorer). Basically, CrashIE is a hater site for Internet Explorer that crashes said browser or at least makes it enter a heavy CPU load cycle. The "solution", they say, is to use better browsers, and you get a nice selection of Opera and Safari and, just to be politically correct, a FireFox icon at the bottom of the page, in between the two other sites that the guy did.

Haters always lose in the end, and this Stanley Shilov will be no exception. No wonder that on his blog he is discussing using Java and Firefox on Ubuntu systems. His extensive experience with Internet Explorer is the thing that probably made him find the hidden bug that a browser will crash if you put it in an infinite loop.

How much of an asshole can you be in order to make a site that will crash some random's guy browser in order to advertise your stupid spellchecker site? BTW, I am writing this in Google Chrome, making said site useless, since it spell checks automatically in any textarea element. Is that the reason what there is no Google Chrome icon on CrashIE?

Bottom line is that you suck ass, Stanley! When I was happy using IE3.0 you weren't even born. I hate you! ;) And, BTW, this is NOT the kind of site that the CodeProject newsletter should have linked to. I am very disappointed in you, guys!

and has 0 comments
You have to excuse me, I've found this blog that I like, Ars Technica, and I can't seem to stop linking to stories there. This one is just funny: UbiSoft released their newest Assassin Creed game and, in a deluxe version, they included a bunch of extras, including the complete soundtrack for the game. However, when looking closer to the ID3 tags for the songs, it was discovered they were pirated versions, distributed on torrents after being taken from the collector's edition of the game.


There is a "theory" that piracy is enhanced by the fact that it is so easy to use pirated content and so damn annoying using the official, paid, version. So easy, it appears, that UbiSoft people found it more efficient to download the pirated version than to go through inner channels to get the songs. They essentially pirated themselves! If that doesn't make you smile a little, you must not be a geek :)

As a small joke I've added a new feature to the blog. If you don't see it, it will see you, hee hee! Warning: the eyes are not visible when the pages first load, only if you spend some time reading.

I was doing some tests with localization in a WPF application and I've found that the way to change dynamically the language for a control (and all of its descendants) is to set the Language property. In my application at least, the propagation of change in the entire subtree took a few seconds. As I found out, the way to refresh the language is to actually remove and readd children to control trees. This is probably why simply changing the Thread culture does not work, what the Language property is for and explains why the error was thrown.

During the Language change on the Window object a validation from one of the custom controls there threw a weird Exception: Cannot modify the logical children for this node at this time because a tree walk is in progress.. What could it mean?

This link on StackOverflow says something vague about a bug in the Silverlight charting toolkit, but I think that is wrong, or at least not the root of the problem. I looked through the Reflectored .Net sources and found that this exception is thrown in AddLogicalChild and RemoveLogicalChild methods in FrameworkElement and FrameworkContentElement when the internal property IsLogicalChildrenIterationInProgress is true. Apparently, this property is set to true whenever am internal class DescendentsWalker<T> is "walking" the visual and/or the logical trees. What is this class?

DescendentsWalker<T> seems to be a class used by the WPF framework to find resources, resolve property values, find ancestors/descendants in the control trees, propagate events, etc, so it is a very important low level class. Either because of a bug or exactly in order to prevent one, an error is thrown if someone is trying to change the structure of the tree while something is walking the tree. That is what the error represents.

Now, the only question is how to solve it. The validation code was trying to change the content property of a control, thus modifying the control tree. As such, I do believe this is a slight bug in the framework, as changing the tree could just wait until the tree walk is over, but still. A simple try/catch solve it locally, but what should I do to prevent such errors in the future for all of my code? I can't pad my code with try/catch blocks.

I've tried using Dispatcher.Invoke for the change, it didn't work. The IsLogicalChildrenIterationInProgress property itself does a this.ReadInternalFlag(System.Windows.InternalFlags.IsLogicalChildrenIterationInProgress); and _flags is obviously a private field of the FrameworkElement or FrameworkContentElement class that is never encapsulated into a public property. So I can't check for it in a simple way. Of course, one can always catch Dispatcher.UnhandledException and just set e.Handled to true if this error occurs (Oops, a minor worldwide tremor caused by developer shudder!), but I don't see another global solution.

Well, at least I've thoroughly examined the problem and its causes and possible solutions, however bad or annoying they may be. If you know of a more elegant solution for it, please share it. As this is the only time I ever got this error, I believe it is something that can be handled locally, but only if one knows how to test for it. So, change the Language of your windows or do other lengthy tree walks in order to protect your application from this bug.

Well, I wanted to convey something like the scene where Mr. Fancy Pants gets the icecream, but still, I passed the MCTS 70-511 exam: Windows Application Development. The grade is a shameful 982, because of a question regarding a DataGridView and a DataSet with related tables (as well as a microwave oven and non-dairy creamer), but it is still a decent pass.

Even if I knew most of the stuff already, some materials for study were: the awful MCTS Self-Paced Training Kit for 70-511 and a post from James J. Foster's blog.

MCTS Self-Paced Training Kit (Exam 70-511): Windows Application Development with Microsoft .NET Framework 4 is a terrible book. It tries to cover the entire subject of Windows application development, so that means it must explain both Windows Forms and Windows Presentation Foundation concepts. Not only that, but it must do it in a consistent, comprehensible way, avoiding confusion between similar ideas that often overlap. It fails miserably on all counts, probably because it can't be done in a single book, but also because it is a jumbled mess that no one that is unfamiliar with the concepts there can actually learn from. I wonder, was poor Matthew Stoecker presented with the questions in the exam and asked to write a book to cover them? Because that would explain a lot. The 70-511 test itself it not much smarter.

The book starts with Chapter 1: Building a User Interface, in which it presents some basic WPF concepts like controls, resources, styles and triggers. If well written, this could have been a good start. It then continues with Chapter 2: Working with Events and Commands, containing stuff about events and commands, obviously, but then animations! So a novice at desktop applications now has to suddenly contend with animation. Maybe it was a slip up, so let's try Chapter 3: Adding and Managing Content, talking about brushes and the visual tree, even transformations, but then it goes into using MediaElement and MediaPlayer to play sounds and video.

Chapter 4: Windows Forms and Windows Forms Controls starts talking about Windows Forms, but in a completely new way and structure than the WPF part. It talks about modifying properties in Visual Studio, it describes the controls, one by one, with properties and all. It's like a completely new book. Chapter 5: Working with User-Defined Controls starts with Windows Forms, then it tries to explain Control Templates in WPF, then jumps back to user controls, this time in WPF.

The book switches to data in Chapter 6: Working with Data Binding, explaining the WPF Binding mechanisms including validation, and in Chapter 7: Configuring Data Binding it talks about various data sources and DataTemplates, also for WPF. Then it moves back to Windows Forms, Chapter 8: Working with Data Grids and Validating User Input, which starts talking about data bound controls in Windows Forms, but then it goes on and on about the DataGridView. It goes on by combining in a single subchapter Windows Forms validation and IDataErrorInfo in WPF.

Before you know it, in this whole confusing bunch of thrown facts, with no structure or plan, you go through Asynchronous Processing (using BackgroundWorker and delegates, but not Tasks!), Globalization and Localization (yeah, that is the important part) and integrating Windows Forms and WPF together, all in Chapter 9: Enhancing Usability. Chapter 10: Advanced Topics manages to mix together security, application settings and Drag and Drop. Chapter 11: Testing and Debugging WPF Applications was, I think, the most decent chapter, but still kind of frankesteined together from different sources, while Chapter 12: Deployment, talked a bit about Windows Installer and ClickOnce.

Conclusion: messed up as a whole, messed up in each small part, it's a fractally messed up book! You even get "chapter summary" points that were not covered in the actual chapter. I couldn't wait for the book to end, but I've managed to read it all. On Monday I am taking a test on this, for the 70-511 exam and I am really not sure how it's going to work out. Luckily for me I knew most of the concepts covered in the book from personal experience so we'll see how it goes.

and has 1 comment
I started writing a long argumentative post about design patterns. But what is the use? They are abstract concepts that get cemented in the development industry by some influential people or some widespread adoption. Then you get to learn them in order to answer stupid job interview questions. In real life you either don't use them or they are abstracted in the programming language or framework you are using.

You dream of getting to work on your own framework so that the entire company would see how awesome you are, but you will only get to write stupid code for web shops and CRMs. And your life gets to its apogee when the architect who does the company framework or some company middle manager/owner tells you how important they are to know for your work. And you blame yourself for being so stupid and vow to spend your entire free time learning, and you read and you go to the water cooler and say "Hey, Bob, did you know the Adapter pattern is so cool?" and Bob looks at you like you are from another planet and goes to fix the latest "Font is too large in the toolbar menu" bug.

Bottom line: it's a chicken and egg thing. They need you to know as much formal programming stuff as possible so you can improve their crappy work and learning environment, while you need to work in a good work and learning environment so you can get better. The result is always an average soup of meh!

Just a month ago, overwhelmed by a number unbelievable claims from companies and lawyers at that time, I wrote the blog entry titled Ridiculous self importance, relaying what I thought of those claims. And yet now I am forced to write another one. See what happens when people don't read my blog? :)

Let's start with the already blogged move by Red-Gate to unilaterally rescind a promise they took to keep Reflector free. Just follow the link to the story for details, but keep in mind the general idea: a software company makes a promise that is completely ignored later on.

Moving on to EA Games. Like any commercial company that reaches a certain critical size, they become assholes. I am talking about the game Dragon Age II, a game that EA Games promised in the forums it would not feature SecuROM, but it does (read article here)! The company also completely fails to notify the game buyers that the feature is there. Another article then tells us the story of a player that bad mouthed the company and got banned from the company forums for 72 hours. But during that time, the Dragon Age II game he bought could not be played, either. That really sounds a bit 1984, doesn't it? The screen device watches what you say and then punishes you. Welcome to the future!

The last article (one can only hope) in the asshole saga is about Twitter. This service that I could never find a use for has grown exponentially because of two factors: the social paradigm and the API that allowed people to make their own software on top of Twitter. Having reached the critical size, Twitter now publicly announced that it doesn't need third party applications anymore. "Thank you, guys, but we will be making the money from now on. Thanks again!". This is one of the reasons why I distrust the cloud idea. I give enough control to Google deciding what is relevant for my queries (and hosting this blog :) ) to give it or other cloud providers the physical ownership of my business.

I believe these stories are related to the recent efforts to regulate the Internet and not only that, but also how you will be permitted to use your own computer or mobile device. Just like weird events before World Wars, they are only the predictors of an era in which the web will be increasingly controlled and will become just another commercial platform for companies. These events that look morally and logically absurd are attempts to shift the regulatory framework in a direction or the other, while we remain blissfully ignorant of the battle over our heads.

Hmm, mentioning 1984 and predicting World Wars... am I becoming paranoid? Well, I hope so, because the alternative is that I am right.


The book (MCTS Self-Paced Training Kit (Exam 70-503): Microsoft® .NET Framework 3.0—Windows Communication Foundation) covers just about everything there was in the .Net 3.0 version of WCF. As I was saying in a previous post, in order to "upgrade" to a 4.0 version you need to read about the router services, service discovery and simplified configuration.

What I liked about the book is that it is a no bullshit, yet comprehensive reference to the tasks for which one would use Windows Communication Foundation. I found it easy to read and comprehend and, most of all, easy to remember.

Don't worry, it's ok now, my years old computer that I can't get myself to replace. The experience has been very educational and I want to share it with you. Here is what happened: the computer that I keep always on started making strange clanking sounds; they came from the HDD. I got a little concerned, but I didn't have the time to address the problem right then. The computer seemed to work fine so I continued (btw, don't you do that in the same situation :) ) to work on it. Finally the computer reset itself and it wouldn't boot. Or, better said, it would boot for a second and restart. I checked the hard drive cables, I removed and cleaned the processor cooler, I vacuumed the power source. Nothing worked. I took the IDE drive from an older Duron computer and tried to boot with that one. It didn't work either, but then again, I had no guarantee that the old drive was functional.

I was really upset. I had an exam to take, all my data was lost, I had a full schedule for days and, being the end of the month, not enough cash to buy a computer right away. And I needed bank loan formalities like I needed an anal probe. But, I thought, maybe I can take the hard drive at work and copy the data, or at least part of it. And it worked. In fact, the drive worked so well at work that I began to doubt that the HDD had any problems. What could it be? I did have a feeling that it might be from the processor cooler that I had removed to clean. After years of use, the thermoconducting gel that transferred heat from the CPU to the metal radiator was only a dusty crust. I decided to buy a cooler or at least some gel.

Of course, any computer shop that was near my work did not have coolers for my old Athlon processor, so I bought gel and then (to be sure) also a cooler for a more recent processor type. I went home, saw that the radiator was way too large for the processor so I removed the fan from the new cooler and placed it (using ingeniously twisted wires) on the old radiator, then applied it with new gel on the processor. And the computer began to work with no drives attached to it. However, attaching the drive would make it reset itself again.

The culprit was, I assumed, either the motherboard (oh no!) or the power source. I removed the old power source from the Duron computer and replaced it on the Athlon computer. And it worked! With wires and thermoconducting gel I brought my baby back to life. I was better than MacGyver! However, the new source would make a really high pitch sound when I turned the computer off and a loud fan noise when I turned it on. I had no intention of buying a new power source for an old computer, I just wanted to make it work.

So I went to my office and borrowed three power sources that had been replaced with newer models. Went back home... none of them worked. As computer parts go, the power source is both the most solicited and the less standard item. There are unlimited ways a power source can fail and the effects on the computer are always surprising. All three displayed some (different) sort of partial functionality. I was considering opening them up and making a Frankensteinian source from them. I know nothing of electronics, but how hard can it be (vision: me burned to a crisp by an electrical fire after having my heart stop due to electric shock). However, I did remember that the source in the Athlon computer was not the original source. I had replaced it with a newer model a while ago and I had kept the old one. I rummaged through my stuff and found the old power source. It worked, it had a somewhat loud fan, but not that loud and it didn't make any electrical high pitched noises. Saved!

I was congratulating myself on repairing the computer using only the things in my house (all but the new cooler) when I remembered that I had an old 500Gb external drive that wouldn't work unless you applied power from the computer power source to the internal drive in the box. And so I did that (using wires again, because the adapter from the old power port to the new got lost somewhere) and backed up all the data from the drive that I thought died. Now I have a backup!

And if I am here, why not borrow a voltmeter and try to figure out how to wire up this external drive so I can use it without all the wires sticking out of it? So the story continues, as this is what I intend to do. I am a guy, tinkering is in my DNA, and it is so satisfying. Also, it helps seeing the uncomprehending look on my wife's face and the horror in her eyes when she sees more wires. It's fun! :)

Today I went and certified myself as a Windows Communication Foundation developer in .Net 4.0 (exam 70-513). I passed with 1000/1000, which makes me "strong" in all areas of the exam. It was difficult, too, as there is no official preparation material for the exam. You have the old 3.5 preparation book for the 70-503 exam and then you have to guess about the things that are in 4.0. Basically, you can read the book, then study a little about the router services, service discovery and simplified configuration. I found a nice post on James J. Foster's blog detailing the material that needs study, with links. Also, here is a link on MSDN about what is new in WCF 4.0.

The problem with the exam is that I had found the questions (the real ones) on the web. You can download them and simulate exams until you remember all of them and their answers. These are called brain dumps (.vce format) and apparently it is the norm rather than the exception; Cisco exams are just the same. It was too weird to be true and, frankly, the whole point of taking these exams was to actually learn something in a structured way, so I was both hoping that those were the real questions and also that they were not and I will have the opportunity to test myself on what I have learned. Alas, it was not the case. I knew all the questions already and this has soiled the entire experience and grade.

Actually, when you think about it, it makes sense. Microsoft needs to show how serious they are about the whole test thing and ignore the elephant in the room altogether, pretending the answers are not out there. Meanwhile, the companies that organize the tests need those answers public, otherwise nobody would dare come take them and thus pay for them. I mean, really... 50 questions of increasing obscureness and a minimum passing grade of 800/1000 ? Who would even consider testing themselves against sets of four questions where one of them would throw a compiler error, one of them would be completely off topic and the other two were competing on which one gets the most hard to remember, one google-second away configuration setting that would make your brain explode. The ugliest thing is that people learning this stuff normally have little to no chance of getting every question right, even if in the official preparation books you find stuff like "Exam tip: beware questions in this format, as they will always be incorrect, but we will ask them anyway so we can trap you if you didn't read our material".

So, bottom line. I am proud of myself for actually studying for this. I feel I know a lot more about WCF and the powerful ways in which I can make it work for me. But the fact that any kid can download a file, do a dozen test runs on their home computer and then take the test and achieve the same perfect score soils it a little.

Well, this makes me a MCTS. I have 3 more exams to take and then I will become a MCPD. It's like achievements in games, only they are worth something.

and has 0 comments
I was trying a piece of code and I was amazed to see that Math.Round(4.5) results to 4! 1.5 leads to 2, 2.5 leads also to 2, 3.5 leads to 4, as does 4.5. According to the MSDN documentation on Math.Round, The behavior of this method follows IEEE Standard 754, section 4. This kind of rounding is sometimes called rounding to nearest, or banker's rounding. It minimizes rounding errors that result from consistently rounding a midpoint value in a single direction. To control the type of rounding used by the Round method, call the Math.Round(Double, MidpointRounding) overload. That's nice and everything, but it contradicts the description of the method on the same page: Rounds a double-precision floating-point value to the nearest integral value. It also contradicts the behaviour of Javascript's Math.round in Microsoft's browser Internet Explorer.

The solution for this is to use the overload Math.Round(value,MidpointRounding.AwayFromZero). Math.Round(4.5,MidpointRounding.AwayFromZero) equals 5. If you don't know that, like I did after 5 years of .Net, you are pretty much screwed. So be wary of helpful mathematical functions.

and has 0 comments
Update: this is more of an exploration post, not something serious on how to achieve the desired result. For a construct that actually works like a switch, check this post out: Creating a C# construct that acts like a more complex switch/case

Many a time I had to select a different behaviour based on the type of a boxed object. The usual solution for this is an ugly stream of if statements that check for the type and then return if found. A while ago I was writing about the dynamic keyword and how it can be used to declare a method for each specific type that is then executed from a simple method having the boxed object as a parameter. Some people liked it, some did not. What would have been nice is a switch statement that works with types, something like:
switch(obj.GetType()) 
{
case typeof(MyClass1):
// do something with obj
break;
case typeof(MyClass2):
// do something else with obj
break;
default:
throw new NotSupportedException();
}
Alas, this is impossible in C# due to the fact that typeof(class) is not considered a constant, but a method call.

What I am about to present to you is in no way a best practice, but something that I've cropped up in my wild musings on code: why not use the try/catch blocks as a switch statement? The idea is simple: create a generic Exception class then throw it based on the type of the object, then catch the specific generic Exception class. Here is the code, enjoy:
class Program
{
public class BaseClass
{
}

public class InheritedClass1 : BaseClass
{
}

public class InheritedClass2 : BaseClass
{
}

public class TypeCatchBlockException : Exception
{
public static void Throw(object obj)
{
doThrow((dynamic)obj);
}

private static void doThrow<T>(T obj)
{
throw new TypeCatchBlockException<T>();
}

/*public static void Throw(object obj)
{
var type=obj==null?null:obj.GetType();
var exceptionTypeName=typeof(TypeCatchBlockException)+"`1";
Type generic=Type.GetType(exceptionTypeName).MakeGenericType(new[]{type});
throw (Exception)Activator.CreateInstance(generic);
}*/
}

public class TypeCatchBlockException<T> : TypeCatchBlockException
{
}

static void Main(string[] args)
{
showObjectType(new InheritedClass2());
showObjectType(new BaseClass());
showObjectType(new InheritedClass1());
showObjectType(new object());
Console.ReadLine();
}

private static void showObjectType(object obj)
{
try
{
TypeCatchBlockException.Throw(obj);
}
catch (TypeCatchBlockException<BaseClass>)
{
Console.WriteLine("BaseClass object");
}
catch (TypeCatchBlockException<InheritedClass1>)
{
Console.WriteLine("InheritedClass1 object");
}
catch (TypeCatchBlockException<InheritedClass2>)
{
Console.WriteLine("InheritedClass2 object");
}
catch (TypeCatchBlockException)
{
Console.WriteLine("Unsupported object");
}
}
}


So there is the simple class TypeCatchBlockException that has a Throw method. You have the dynamic implementation as well as the reflection implementation in the commented region. You execute Throw with an object inside a try block and then you add a catch block for each generic version of the exception for each supported type. The last block is reached if none of the supported types were encapsulated into the exception object.

Executing the console application in the code block results in:
InheritedClass2 object
BaseClass object
InheritedClass1 object
Unsupported object


Update: Some people didn't understand this was mostly a joke, so I needed to clarify it. We do need type switches and someone should implement them as a language construct. Throwing exceptions is expensive and the solution in this blog post would not be recommended for any production software.

But here is a nice solution for type switching using lambda expressions: Switching on Types.

and has 3 comments
I am the kind of guy that uses a piece of software until he has a really good reason to change it. I don't try new stuff if I am satisfied with what I've got and I am all for second chances when mistakes are made. After all, I am not immune to the occasional error. However, from this day on I am fully switching from Internet Explorer to Chrome.

Wipe that smirk off your face, I happen to think that IE is a reasonable browser and, given the hypothetical situation where Google would have made their browser compatible with Internet Explorer rather than the standards, you would all curse at FireFox right now. Besides, this is not about the standards at all. Firefox is still not on my list of available choices. It is sluggish, pretentious and buggy.

The thing is that parameters have changed. I now use the browser to quickly get answers to questions or to open RSS feed blog articles. Before I was using it to make ASP.Net applications and web applications and I still think Internet Explorer 8 developer tools are nicer and easier to use than Firebug, not to mention the horrible confusing developer tools on Chrome. Now I am free (partially) of web development and I can rejoice. Also, sites were more compatible with IE than any "standard" browser and it is not the case any more.

The downloading window from Internet Explorer always made more sense to me: have an option of Open, Save and Cancel and do it in another process so you can close the windows that spawned the download. Then it opens or you just go to the file and use it, you know where you saved it. I think this is the only thing I will miss from Internet Explorer, though.

Let's summarise:
  • Chrome opens instantly, letting you write something in the address bar immediately and having autocomplete for search term as well as web addresses. Internet Explorer blocks the CPU for a second or two and, even if it lets you write something in the address bar, it hickups just so that what you wrote is truncated. Firefox is so keen to update every fucking extension that it takes forever to start. Yeah, I know, FF4 is way better. Not compared to Chrome it isn't.
  • Internet Explorer is supposed to support the new Silverlight wonder technology. Unfortunately, every single SL video I open is sluggish, blocks my mouse, doesn't respond to simple commands like Space to pause and arrows to skip and every Silverlight app is alower than their Flash counterpart. I've just wasted one hour today to uninstall with a special tool the older Silverlight version as the installer for the new one brutally stopped with a "generic error message". Well, screw that! I would rather download the videos and run them in a "generic" video player
  • Javascript: it works 8 times faster on Chrome that on Internet Explorer 8. I know they've just released Internet Explorer 9, but it won't work on Windows XP. I have an old computer, it works fine, I want a browser for it without buying a trillion dollar Windows 7 license. You can't provide it for me, Microsoft!
  • HTML5: it is supported in Chrome. If I ever want to start web dev again, I would go with that
  • Extensions: I am not a big fan of extensions as they slow the browser down. I do prioritize speed. However, I have AdBlock Plus installed on Chrome and I couldn't install it (even if I have tried emulating it) on Internet Explorer. Having no flashing nonsense on a page when I am reading the text on it is wonderful. Try it!


I could find even more reasons if I think about it longer. Like a long relationship going bad, you start noticing all the things you hated only after you break up, but break it up I do. So there it is, found me a new girlfriend and she is chrome shiny.

As you know, the uberuseful tool Reflector, originally developed by Lutz Roeder, was bought by Red Gate, with promises that the tool will always remain free for its users. Of course, the first thing they did is to create a commercial version of it with some extra features, but they did keep their word by allowing people to use a free version. Recently they changed their minds again, practically saying "Fuck you, developers!" and asking for money for any version of Reflector. The community was outraged.

Enter Jetbrains, my favourite company, the one that would have replaced Google and Microsoft at the top of the development world if they weren't so busy actually developing. They are also the makers of ReSharper, which you should download and use if you are any decent C# programmer. What did they think of this Reflector fiasco? They've decided to build a version of their own! They did bundle it in ReSharper 6, which is a commercial tool, but then... they promised to make it a free standalone tool! Thumbs up, guys!

Here is the blog entry announcing this: ReSharper 6 Bundles Decompiler, Free Standalone Tool to Follow