Sunday, December 30, 2012

My notebook won’t charge on Sundays!

Hell, yeah, no matter how weird it sounds, it seems true.

The laptop is a HP Envy 14 Spectre ultrabook running Windows 8 Pro. It’s a great device, well-made and pleasant to use. It’s new and runs some 4-5 hours on fully-charged battery while surfing. Originally it came with Windows 7. I’ve upgraded to W8, and this has probably caused the issue – HP  doesn’t officially support W8 on this device.

When I noticed the battery was not charging for the first time, I thought the device was broken. I had already decided to take it to the service centre, but I found it fully charged when came home the next evening. The issue has repeated several times after that, and I've noticed it occurs only on Sundays. It literally turns off at 12 PM on Sunday and turns on at 12 PM on Monday. Confused smile

I’ve searched for a solution, but I’ve only found out that either my battery is dead (it is not for sure), or there might be an issue with Windows power management drivers. I’ve followed the suggestions to reinstall the Microsoft ACPI-Compliant Control Method Battery, but it doesn’t help.

So far I cannot find a similar issue description. I don’t know if this is unique to my machine or reproduces on other devices.

I haven’t contacted HP support yet, but I probably will do. Or maybe I’d better contact the HP notebook battery labor union and negotiate a 7-day workweek.

Thursday, October 20, 2011

Convert .NET DateTime.Ticks to T-SQL datetime

It's not very uncommon to store DateTime values as UTC Ticks count in a bigint SQL Server column (or something equal on other DB engines). This gives total control over timezone shifts, DST, UTC/Local time problem, etc. Ticks are ticks.
While it's rather easy to work with this in .NET code:
var valueToStoreInDb = dateTimeVariable.ToUniversal().Ticks;

var dateTimeValue = new DateTime(ticksFromDb, DateTimeKind.Utc);
Accessing the data from SQL (e.g. in a simple SELECT query written by a DBA) is painful, because huge numbers are absolutely not human-readable.

The obvious way to solve the problem is creating a User-Defined Function on DB server. I tried to search the internet for such functions and have found only limited-precision solutions, like this one which is accurate to the minute. I don't know why the author did not implemented a full-precision solution. Perhaps, it was due to the fact that minute is the lowest unit the number passed from the 1900 to today can be stored in an int variable. However, it's not difficult to write a function with top possible precision for datetime type.

Thursday, December 9, 2010

Migrating TFS 2008->2010: Where are my links?!

I've recently been asked to write a simple tool that checks if all links between TFS WorkItems and ChangeSets have migrated successfully from TFS 2008 to TFS 2010, and migrates missing links if necessary.

The interesting thing was why did that task even arise. At that moment, it seemed that the tool that migrates projects from TFS 2008 to 2010 somehow looses all links. Yes, the tool said migration was completed, but there were no links. Later, by the time I completed the tool, the links had somehow got to the destination. Autopsy has shown that after the tool had completed, it started a Windows service, which apparently completed the migration in background.

Those guys in TFS team... are they mad?

Thursday, December 2, 2010

WPF: Color your items with data-binding

I've recently run into a tricky task while implementing a Windows Eplorer-like browser feature in the project I work at. I have a collection of data items (in my View Model) that have a 'Type' property of enumeration type, and I want to display them in a ListBox. The tough requirement is that items should be colored depending on their Type property value, including custom Foreground and Background of selected items (for various combinations of IsSelected, IsSelectionActive and IsEnabled properties).

Wednesday, December 1, 2010

WPF Hyperlink default style

Tried to find the default style of a Hyperlink and discovered that neither Google, nor Expression Blend or ShowMeTheTemplate have it. So I had to disassemble WPF assembly with .NET Reflector+BAML viewer. Here is the style for Aero NormalColor theme, just for reference purposes:
   <Style x:Key="{x:Type Hyperlink}" TargetType="{x:Type Hyperlink}">
            <Trigger Property="IsMouseOver" Value="true">
                <Setter Property="Foreground" Value="Red" />
            <Trigger Property="IsEnabled" Value="false">
                <Setter Property="Foreground"
                       Value="{DynamicResource {x:Static GrayTextBrush}}" />
            <Trigger Property="IsEnabled" Value="true">
                <Setter Property="Cursor" Value="Hand" />
        <Setter Property="Foreground" Value="Blue" />
        <Setter Property="TextDecorations" Value="Underline" />
Oh, a slight surprise for me was that the hyperlink colors are not bound to any SystemColor members. No, they are just Red and Blue nailed in the style. Why?....

WPF ItemContainerStyleSelector unlimited: going data-bound

Use case
You have a View Model which contains a collection of items. In View, items are presented in an ItemsControl, say ListBox, and containers for each item should have different styles. Styles are selected depending on some property of view model items, e.g. Type property. An example of such situation is when each Type has its own combination of colors.
At this point, the task can be easily solved using a StyleSelector which encapsulates the selection logic. It's rather simple, so I won't discuss it.
To make task really interesting, let's require the selection logic to track changes in data items like Bindings do, i.e. if the value of the Type property changes, Style should be changed in the moment.