Monday, 17 October 2016

Possible temporary solution for Windows 10 freezing after short idle

I actually cannot remember when this happened but my Windows 10 starts to freeze after I just left the computer for a very short time, probably less than 2 or 3 minutes.

Initially I thought it is my SSD where the windows is installed that has issue but sooner I noticed that as long as I keep using the computer then it does not freeze.

There are discussions, for example:

http://www.theregister.co.uk/2016/08/16/windows_10_anniversary_update_ssd_freeze/
http://www.pcworld.com/article/3107273/windows/windows-10-anniversary-update-freezing-on-you-microsofts-looking-into-it.html

More reading reveals that this could only happen to users that are booting Windows from a SSD drive (still unsure) which is my case.

And a representative from Microsoft suggested the following:
http://answers.microsoft.com/en-us/windows/forum/windows_10-performance/windows-10-may-freeze-after-installing-the/5a60d75d-120a-4502-873c-8bfec65c82d0?page=1&auth=1

To me they looks too complex and I like simple stuff so below is my temporary solution until Microsoft have a fix for it.

Copy the following code and save it in a batch file (e.g. anti-freeze.bat) on the booting SSD drive and when you think you are going to leave the computer for a while, just double click to bat file to preventing Windows from freezing.

@echo off

if "%1"=="done" goto start
start "" /min %0 done
exit

:start
cls
echo Press Ctrl + C to quit...

cmd /c start notepad
timeout /T 5 /NOBREAK
taskkill /F /IM notepad.exe
timeout /T 5 /NOBREAK

goto start

What this does is it fires a windows command prompt, make itself minimized and then open/close notepad every 5 seconds. This works for me and I have left my PC on for the whole night and it is still alive.

Let me know if it works for you too! Have a nice day.

Thursday, 6 February 2014

Annoying issue with ASP.NET ImageButton inside UpdatePanel

I personally do not like to use UpdatePanel at all however in some cases I need to maintain the code written by others. One annoying problem I encountered today is that in IE10 or IE 11 the ImageButton placed inside an UpdatePanel when clicked just does nothing and the following error is thrown in IE's error console:

Sys.WebForms.PageRequestManagerServerErrorException: Input string was not in a correct format.

I reckon this is a bug within the framework and an easy fix is just to change the code to use LinkButton instead and put the image inside.

Friday, 6 December 2013

Repeat a string N times in C#

Sometimes I want to repeat a given string several times for formatting purposes. The following code can be either made into an extension method with slight changes or a utility method:

string.Join("", Enumerable.Repeat("a", 8).ToArray());

The code above repeat "a" 8 times and returns "aaaaaaaa".

Tuesday, 22 October 2013

Hating hosts file?

Sometimes I found that it is pretty annoy to keep editing the hosts file to add a new development domain on my local machine and a colleague told me that there are several domains (and their sub-domains) that automatically loop back to 127.0.0.1 (localhost), for example, 42foo.com, localtest.me, lvh.me, etc.

I personally like localtest.me as it seems more logical to me. So now when I am developing a new site, I could use something like newsite.dev.localtest.me directly without adding an entry in hosts file.

Wednesday, 16 October 2013

Checking null for Sitecore.Data.ID

The Sitecore.Data.ID class exposes a IsNull property which the documentation says it checks whether the instance is null. However I discovered that when the ID variable is actually null it throws Object reference set to null instance of object exception.

So it is better to the following code in this case (perhaps always?):
Sitecore.Data.ID.IsNullOrEmpty(myID)

Sunday, 13 October 2013

Linq.js - where to filter on sub collections

Linq.js (you can find more info on CodePlex - http://linqjs.codeplex.com) provides convenient way manipulating your javascript collections (mainly arrays) and exposes it as LINQ that we are familiar with in .NET world.

One issue I found that it is hard to find information on how to achieve it is that when I want to apply the where clause on sub collections.

For example, given the following array:

var cars = [
  {id: "F001", make: "Ford", model: "Fiesta", 
    colors: [
      {id: 1, name: "Red"},
      {id: 2, name: "Blue"}
    ]
  },
  {id: "T001", make: "Toyota", model: "Corolla",
    colors: [
      {id: 1, name: "Red"},
      {id: 3, name: "Silver"}
    ]
  },
  {id: "T002", make: "Toyota", model: "Yaris",
    colors: [
      {id: 1, name: "Red"},
      {id: 3, name: "Silver"},
      {id: 4, name: "White"}
    ]
  }
];

It is pretty easy to find cars that are made by Toyota (note that the code examples below are using linq.js 3.x:


var toyotaCars = Enumerable.from(cars)
                           .where("c=> c.make == 'Toyota'");

And we will get Corolla and Yaris.

However, when I want to find cars that are available in blue and how do I suppose to do that?

People might have tried the following already:


var blueCars = Enumerable.from(cars)
                         .where("c=> c.colors.name == 'Blue'");

Or

    .where(function(c) {return (c.colors.name == 'Blue');})


Unfortunately this is not how it works and you will get nothing. Obviously linq.js is trying to work as close as to .NET LINQ but nothing is perfect. So to achieve what we want is the following:


var blueCars = Enumerable.from(cars)
    .where(function(o) {
      return (Enumerable.from(o.colors)
                        .where("c => c.name == 'Blue'").any());
    });

Here you go, we will get Ford Fiesta!