Sunday, October 25, 2009

NotYetImplimentedException

This is just a little thing, and it's something that has since been fixed, but it kind of illustrates the lack of communication or thought between different parts of Microsoft teams. When I became an MVP, I was almost immediately whisked to a Microsoft summit. I believe VS2005 was in development at the time. During the summit, the person in charge of the VS IDE gave a presentation. I asked her at the time why it is that visual studio will create stub methods on a right click, so I can do

myClassInstance.NewMethod();

then right click and create a stub to then navigate to, to add this method, and the default method would throw a generic exception. The point being that a 'NotYetImplimentedException' existed in the framework and is what should be thrown here. She 'took a note'. However, this was not rectified in 2005 ( although I did get sent a nice paperweight that says 'thanks for helping us improve Visual Studio 2005', the fact is that not one bug I reported was corrected ). I believe it was fixed in VS2008, but the point is not so much that they didn't immediately make a tiny change to a product in development to make it act in a sane manner, it's the fact that they didn't do what I was suggesting in the first place. Like the issue with Visual Studio not working in Vista without setting it up to run as admin, or any number of other issues we all encounter, the problem is that the teams at Microsoft don't seem to talk to one another, don't seem to care about using the framework as a whole consistently, and don't seem to care about how their product will work in relation to the rest of the Microsoft product base.

Friday, October 23, 2009

Default behaviour/WMP control

When I was an MVP, one of the first things I did was ask the C# team for default values on variables, like C++ has, and VB.NET has. They would get tied in knots over how complex it would be. We are finally getting this feature. In the meantime, if I had a method and I wanted to extend it, I'd do something like this:

old method :

bool DoSomething(string name, int age)

new methods:

bool DoSomething(string name, int age, bool doSomethingExtra)
{
...
}


bool DoSomething(string name, int age)
{
return DoSomething(name, age, false);
}

In other words, you need to keep your default behaviour for existing clients of the code, and then allow changes of the code for any client who wants to use the new functionality. In any situation, if you want to extend something that exists, you need to make sure you don't break it for people already using it, right ?

Our WPF application uses the Windows Media Player control, I've already explained why in the past. We just wrapped up our installer for our new version and shipped it. Shortly after release, we started to get reports from people that the player in our app was showing the WMP controls. It turns out the person who wrote that code, never added code to set the uiMode. I have searched and can't find any way to set the default value for that, but I have had it happen on one machine, further confirming that a windows update has changed windows media player so that if you don't set a uiMode, the default is no longer none. This is major for us because it also means the control catches right clicks, which we use when we put the control in full screen mode.

On top of that, a video which plays fine on all of my test machines, some of which are completely virgin, is reporting that 'Windows Media Player cannot open files of type wmv'. Now, all our videos are WMVs, but either way, WMP can't play WMVs ? Again, it's clear by the fact this is happening on computers where before everything was fine, that a windows update has basically changed how the windows media player control behaves, and broken our app. I am shipping an update that sets the uiMode, and we should have done that in the first place, but either way, if you do an update, you should make sure you're not going to break existing clients. That's just common sense.

Update: I installed IE8 ( I actually asked it to install 5 times, it would not install until I selected to participate in the customer experience improvement program ), and now I can reproduce the UI issue. The video in question still plays fine, but it looks to me like IE8 is the cause of the other issue. I need still to install XP SP3, I've avoided that since it completely trashed one of my AMD based machines, I'll install that next ( my Mac is Intel based ), and see if that creates the issue with the file not playing.

(update) I have updated my Vista machine to the hilt ( 100+ updates, took an evening and a morning to do ), and I can't repro the issue with the file not playing, so I assume that was actually an issue with a single install, and not a Microsoft issue at all. As I updated my Vista machine, I once again saw our UI change from no UI over WMP to the full UI, as reported above. I am just rolling out the patch now to fix this issue for our users.

Tuesday, September 15, 2009

Not every computer is online

I had to test some installers yesterday. Four of them. I needed to run them on a virgin machine, so I could test all the installation of prerequisites. So, I wiped my notebook, and put Vista on it, using the factory install DVD. I hoped to use the XP on the machine, but I'd never activated it, and despite having a license, activation failed. So, I went the quickest route open to me.

Vista is frustrating, as always. In fact, after running the uninstaller once, I found that the control panel stopped working. It would flash up and disappear. If I ran in safe mode, it works, but gives an obscure message about permissions when I try to uninstall something. For the record, I installed .NET 3.5, Acrobat, and my program, so one of those screwed it up. I was able to keep working by running Programs and Features ( because they HAD to rename it in Vista, or it may have been easy to find the first time I used it ), from My Computer.

So, my installer is running, and taking *forever*. I know the stupid .NET 3.5 install claims to be downloading, even tho I have the full version on the DVD. I suddenly realise, it's trying to connect to download, even though it doesn't need to, and timing out five times before noticing it has the data locally. Then it moves up a bit, and repeats the sequence. It's bad enough that our clients are shown a screen saying that .NET 3.5 is downloading, when it's not, but this is just the height of stupidity in my opinion. I was at a family dinner, testing, so I didn't have the option of connecting to the web, which slowed down my work considerably. Another example of Microsoft's inability to see past their own assumptions, and consider what the possible situation of real users may just well be.



Tuesday, September 1, 2009

Consistency

So, I am using the Windows Media Player control within my WPF project, because the built in control is too slow. My problem is, I want the control to not play the video right away, but to have it loaded, so there's no moment of black screen when you press play. The only way I can see to do this, is to turn off the volume, tell it to play, and when it starts, pause it and turn the volume back on. Which is, of course, messy.

What is also messy, is that when I search the MSDN, the media play control has a ton of different types of volume settings. One allows for the volume to go between 0.0 and 1.0, with 0.0 being off. At other times, the windows media player control apparently has settings between 0 and -10,000, where 0 is full volume. When I find the right documentation, for the right version of the object, it tells me that the settings property "... gets an IWMPSettings interface that provides a way to modify various Windows Media Player settings." What is a IWMPSettings interface ? What does it expose ? I did find a link which would tell me that, and, for the purposes of the COM control, under C#, the range is 0 to 100. I think. I mean, it's not really clear enough for me to be sure, given all the other options I have been given. I'll try that and see.

My point today is - why does Microsoft have to do the same thing 100 different ways ? Why can't they just decide on something and stick to it ?

Monday, August 17, 2009

How is this possible ?

OK, so I have a media player control in my WPF app. For reasons that I cannot fathom, WPF does not let me easily find out the native size of a video, so I have added a map of video paths to sizes. I have built this map, and when I load a video, I grab a screenshot of the first frame. After that, you can play and pause the video, when you pause, it goes to the static screenshot, so that it will appear in screen grabs, which we print and email.

if (MediaAccessManager.Instance.VideoSizeMap.ContainsKey(srcPath))

This is the line of code I use to check if I have a size for this video in my map ( users can import their own videos and then I need to just use DirectX via a third party dll to find the size. I don't do this for my own videos, because sometimes the size reported, is incorrect. Here is the issue. The first time this code runs, I get an exception, stating that the key in question already exists in the collection. Believe me, I have read this code over and over, there is NO code here to put anything into the collection, nor does this crash after the first time. The only possible reason I can think of, is that perhaps the collection does not blow up when I put in a duplicate key, it blows up the first time I try to access it after the fact. However, I call ContainsKey before adding each item, and I have written some code to further confirm that I have no duplicate keys. I also wrote code to deliberately add the same key twice, and it threw an exception at the point of adding the item, as you'd expect.

I really don't have time to debug this, so I've added the block of code above in it's own try/catch. The first time this runs, it throws an exception, but then it does it again right away, and the second time it works fine, causing my code to work perfectly. It's just not at all pretty.

Sunday, August 9, 2009

Why can't I drag and drop a csproj file ?

I was reminded again today that if I drag and drop a sln or csproj file onto the main window of VS2008, I get a message box saying that I am not allowed to do that.  It doesn 't even open File/Open for me, just tells me I need to do it.  Why ? I can't think of any reason for them to insert a message box instead of a jump to the code that opens a project ( with a prompt if they would feel better that way ).  Whose smart idea was that ? It's just a pain.

Wednesday, August 5, 2009

Path.Combine is essentially useless

One of the great things about .NET, coming from C++, is all the stuff that is built in.  Need to send an email ? Sure thing.  Want to use regular expressions ? Go for it.  It took me a while to learn that things I expected to write in C++, I could look for in the library and often find there already.

The System.IO namespace has a Path class, which is used to manipulate file paths.  Things like 'GetFileNameWithoutExtension' are very, very useful.  Some things are a little counter intuitive, such as Path.GetDirectory walking up the directory tree if the string you have is a directory already, but overall, it saves a lot of work.

One of the things I use the most, is Path.Combine, which takes two fragments and merges them to make a path.  In the past, I'd be checking if one string had a trailing slash, if the other had a leading slash, etc.  Path.Combine takes care of that for you.  Right ? Not quite.

There's a couple of quirks here.  To illustrate, the following table has three columns.  The first two are the arguments passed into Path.Combine, the third is the result.


c:\path\dir\file.txtc:\path\dir\file.txt
c:\path\\dir\file.txt\dir\file.txt
c:\pathdir\file.txtc:\path\dir\file.txt
c:\path\dir\file.txt\dir\file.txt
c:dir\file.txtc:dir\file.txt
c:\dir\file.txt\dir\file.txt

The first thing to notice, is that if the second string starts with a \, then you get the second string back verbatim.  This is the issue that hit me in the past.  I assumed that this method existed so no matter what slashes happened to be in the two strings, they would get joined into a single path.  As you can see, this is not so.  Now, I assume there's a specific case for which this behaviour is desirable, but it's not the most obvious one to me, and if there's a reason for it, surely the method could have an overload, or better yet, a method called something in line with the reasoning for not combining these two strings could exist ( a method called Combine, is one I call to combine strings, not to SOMETIMES Combine them ).

The second one is more interesting.  If my first string is a drive letter, with no slash in it, then no slash is added.  I just did a test, I have a file called c:\procs.txt.  File.Exists (@"c:procs.txt") returns false, File.Exists(@"c:\procs.txt") returns true.  So, it seems to me that the slash is needed, but Path.Combine does not add it.  

Overall, this method is basically broken as far as I am concerned, and I have rolled my own version to use instead.  It basically makes sure the first string has a \ at the end, the second doesn't have one at the start, then calls Path.Combine, just out of spite ( given that at this point I could just concatenate the two strings and be done with it )