Wednesday, 31 December 2008

Blackberry 8800 video encoding - How to

Download this free encoder: http://www.erightsoft.com/SUPER.html
The download link is very hard to find, so its here

Use these settings:
Output container: AVI
Video Codec: DIVX
Audio Codec: mp3
Encoder: MEncoder

Video settings
Size: 320:176
Aspect: 16:9
Frame/sec: 12.5 (pal) 14.985 (NTSC)

Audio settings
Sampling freq: 22050
Channels: 2
Bitrate: 64
Audio stream: default

Enjoy!

Friday, 19 December 2008

A generic singleton - Part 2

Here is the critique and why there is a better solution to the article: A generic singleton

Well, it isn't really singleton - since you can't control T, there can be as many T instances as you like.

The code does check for non-public constructors, which is good. However, this is a check which is only performed at execution time - there's no compile-time check, which is a strike against it. It also relies on having enough access to call the non-public constructor, which adds some limitations.

In addition, it doesn't prohibit internal constructors - so you can end up with non-singletons.

There is an excellent article on singletons at http://www.yoda.arachsys.com/csharp/singleton.html

Here are two proper implementations:

public sealed class Singleton
{
    static readonly Singleton instance = new Singleton();
 
    // Explicit static constructor to tell C# compiler
    // not to mark type as beforefieldinit
    static Singleton()
    {
    }
 
    Singleton()
    {
    }
 
    public static Singleton Instance
    {
        get { return instance; }
    }
}

Or simply:

public
class Singleton
{
    private static Singleton _instance = new Singleton();
    private Singleton() {}
    public static Singleton Instance { get { return _instance; }}
}

Both of these classes removes the need for locking, as a static constructor is thread safe.

Surprising SQL speed increase - Part 2

Part 1 is availble below

More generally speaking, you should never use a function on the LEFT side of a WHERE clause in a query. If you do, SQL won't use an index--it has to evaluate the function for every row of the table. The goal is to make sure that your where clause is "Sargable"

Some other examples:


Bad: Select ... WHERE isNull(FullName,'') = 'Ed Jones'

Fixed: Select ... WHERE ((FullName = 'Ed Jones') OR (FullName IS NULL))

Bad: Select ... WHERE SUBSTRING(DealerName,4) = 'Ford'

Fixed: Select ... WHERE DealerName Like 'Ford%'

Bad: Select ... WHERE DateDiff(mm,OrderDate,GetDate()) >= 30

Fixed: Select ... WHERE OrderDate < DateAdd(mm,-30,GetDate())


Bad: Select ... WHERE Year(OrderDate) = 2003

Fixed: Select ... WHERE OrderDate >= '2003-1-1' AND OrderDate < '2004-1-1'

Thursday, 6 November 2008

The 3 Minute Management Course

Lesson 1
A man is getting into the shower just as his wife is finishing up her shower, when the doorbell rings. The wife quickly wraps herself in a towel and runs downstairs. When she opens the door, there stands Bob, the next-door neighbour. Before she says a word, Bob says, "I'll give you £800 to drop that towel." After thinking for a moment, the woman drops her towel and stands naked in front of Bob. After a few seconds, Bob hands her £800 and leaves.

The woman wraps back up in the towel and goes back upstairs. When she gets to the bathroom, her husband asks, "Who was that?" "It was Bob the next door neighbour," she replies. "Great!" the husband says, "did he say anything about the £800 he owes me?"

Moral of the story: If you share critical information pertaining to credit and risk with your shareholders in time, you may be in a position to prevent avoidable exposure

Lesson 2
A priest offered a Nun a lift. She got in and crossed her legs, forcing her gown to reveal a leg. The priest nearly had an accident. After controlling the car, he stealthily slid his hand up her leg. The nun said, "Father, remember Psalm 129?" The priest removed his hand. But, changing gears, he let his hand slide up her leg again. The nun once again said, "Father, remember Psalm 129?" The priest apologised "Sorry sister but the flesh is weak." Arriving at the convent, the nun went on her way. On his arrival at the church, the priest rushed to look up Psalm 129. It said, "Go forth and seek, further up, you will find glory."

Moral of the story: If you are not well informed in your job, you might miss a great opportunity

Lesson 3
A sales rep, an administration clerk, and the manager are walking to lunch when they find an antique oil lamp. They rub it and a Genie comes out. The Genie says, "I'll give each of you just one wish." "Me first! Me first!" says the admin clerk. "I want to be in the Bahamas, driving a speedboat, without a care in the world." Puff! She's gone.

"Me next! Me next!" says the sales rep. "I want to be in Hawaii, relaxing on the beach with my personal masseuse, an endless supply of Pina Coladas and the love of my life." Puff! He's gone.

"OK, you're up," the Genie says to the manager. The manager says, "I want those two back in the office after lunch."

Moral of the story: Always let your boss have the first say.

Lesson 4
An eagle was sitting on a tree resting, doing nothing. A small rabbit saw the eagle and asked him, "Can I also sit like you and do nothing?" The eagle answered: "Sure, why not." So, the rabbit sat on the ground below the eagle and rested. All of a sudden, a fox appeared, jumped on the rabbit and ate it.

Moral of the story: To be sitting and doing nothing, you must be sitting very, very high up

Lesson 5
A turkey was chatting with a bull. "I would love to be able to get to the top of that tree," sighed the turkey, "but I haven't got the energy."
"Well, why don't you nibble on some of my droppings?" replied the bull. They're packed with nutrients." The turkey pecked at a lump of dung, and found it actually gave him enough strength to reach the lowest branch of the tree.

The next day, after eating some more dung, he reached the second branch. Finally after a fourth night, the turkey was proudly perched at the top of the tree. He was promptly spotted by a farmer, who shot him out of the tree.

Moral of the story: Bull Shit might get you to the top, but it won't keep you there

Lesson 6
A little bird was flying south for the winter. It was so cold the bird froze and fell to the ground into a large field. While he was lying there, a cow came by and dropped some dung on him. As the frozen bird lay there in the pile of cow dung, he began to realize how warm he was. The dung was actually thawing him out! He lay there all warm and happy, and soon began to sing for joy. A passing cat heard the bird singing and came to investigate...
Following the sound, the cat discovered the bird under the pile of cow dung, and promptly dug him out and ate him.

Moral of the story:
  1. Not everyone who shits on you is your enemy
  2. Not everyone who gets you out of shit is your friend
  3. And when you're in deep shit, it's best to keep your mouth shut!

Friday, 3 October 2008

Surprising SQL speed increase

I’ve just found out that the execution plan performance between the following two select statements are massively different:

select * from your_large_table
where LEFT(some_string_field, 4) = '2505'

select * from your_large_table
where some_string_field like '2505%'

The execution plans are 98% and 2% respectively. Bit of a difference in speed then.

There are lots of places in our SQL where we use LEFT(something, x) = 'string'
This should be replaced by the LIKE command for greater speed.

I actually found this out by checking the LINQ generated SQL against my hand crafted SQL. I assumed the LIKE command would be slower, but is in fact much much faster.

Saturday, 13 September 2008

A generic singleton

Taken from: http://andyclymer.blogspot.com/2008/02/true-generic-singleton.html
Changed by Simon Hughes 20th Feb 2008
 
using System;
using System.Reflection;
 
// Use like this
/*
public class Highlander : Singleton<Highlander>
{
    private Highlander()
    {
        Console.WriteLine("There can be only one...");
    }
}
*/
 
public class Singleton<T> where T : class
{
    private static T instance;
    private static object initLock = new object();
 
    public static T GetInstance()
    {
        if (instance == null)
        {
            CreateInstance();
        }
 
        return instance;
    }
 
    private static void CreateInstance()
    {
        lock (initLock)
        {
            if (instance == null)
            {
                Type t = typeof(T);
 
                // Ensure there are no public constructors...
                ConstructorInfo[] ctors = t.GetConstructors();
                if (ctors.Length > 0)
                {
                    throw new InvalidOperationException(String.Format("{0} has at
least one accesible ctor making it impossible to enforce
singleton behaviour", t.Name));
                }
 
                // Create an instance via the private constructor
                instance = (T)Activator.CreateInstance(t, true);
            }
        }
    }
}

Wednesday, 9 April 2008

BypassProxyOnLocal - a bug in .Net ?

If you have a local proxy server on your computer (127.0.0.1) that you wish to use when doing FTP communications, all you need do is set BypassProxyOnLocal = false right?

There may be many important reasons for using a local proxy, such as caching and security etc.

Here's an example:

WebProxy proxy = new WebProxy("127.0.0.1", 3128);

proxy.BypassProxyOnLocal = true;
bool a = proxy.IsBypassed(new Uri("ftp://127.0.0.1:3128"));
bool b = proxy.IsBypassed(new Uri("ftp://127.0.0.1:21"));
// a=true, b=true (ok)


proxy.BypassProxyOnLocal = false;
bool c = proxy.IsBypassed(new Uri("ftp://127.0.0.1:3128"));
bool d = proxy.IsBypassed(new Uri("ftp://127.0.0.1:21"));
// c=true, d=true (???)


Why is the proxy being bypassed when I told it not to?
The local proxy is always bypassed when when the proxy is on a loopback address. Why?

Setting BypassProxyOnLocal to false should NOT bypass the proxy for local addresses. So let's see what MSDN Library has to say about IsBypassed method:

If BypassProxyOnLocal is true, requests to local Internet resources do not use
the proxy server. Local requests are identified by the lack of a period (.) in
the URI, as in http://webserver/, or access the local server, including
http://localhost, http://loopback, or http://127.0.0.1. When BypassProxyOnLocal
is false, all Internet requests are made through the proxy server.

Here is the WebProxy.IsBypassed source code:


public bool IsBypassed(Uri host)
{
GlobalLog.Print("WebProxy#" + ValidationHelper.HashString(this) + "::IsBypassed() destination:" + ValidationHelper.ToString(host));

if (host == null)
{
throw new ArgumentNullException("host");
}

AutoWebProxyState autoWebProxyState;
bool result = IsBypassedAuto(host, out autoWebProxyState);
if (autoWebProxyState==AutoWebProxyState.ExecutionSuccess) {
return result;
}

return IsBypassedManual(host);
}

private bool IsBypassedManual(Uri host) {
if (host.IsLoopback) {
return true; // bypass localhost from using a proxy.
}
return (_ProxyAddress==null && _ProxyHostAddresses==null) (_BypassOnLocal && IsLocal(host)) IsMatchInBypassList(host) IsLocalInProxyHash(host);
}

Tuesday, 5 February 2008

Design patterns

I've recently been on a training course with http://www.developmentor.com/ it's called Code Smarter with Design Patterns in .NET and can strongly recommend it.
I simply love design patterns, and you should to :-)

Monday, 28 January 2008

GRC.COM by Steve Gibson

https://www.grc.com/ is run by Steve Gibson. He has many great utilities and has a Security Now podcast. I can't recommend them strongly enough.

Wednesday, 23 January 2008

DHCP Client service causing my PC to go slow

Recently I’ve noticed my laptop going quite slow. For example, using Windows explorer and clicking on a folder resulted in a 40 – 60 second wait for the folder to show its contents.
Well, I’ve worked out by stopping the ‘DHCP Client’ service my PC goes like lightening again. I'm also using Windows XP.

To fix, you need to delete all Printers that are offline, and remove all mapped network drives.

Why does the ‘DHCP Client’ affect windows explorer? I don’t know, but Microsoft if your reading this, please fix it.

Tuesday, 22 January 2008

What do I use to write software?

I started off with Dbase III, then Borland Turbo C for about 5 years, then when Microsoft Visual Studio v1.0 came out, I moved to that.

I've been using Visual Studio ever since. Coding in C++ and have since moved to C# when that came into existance.

Do I use component? Yes.
Which ones then?


I've also used DevExpress reports, but they peg the CPU to 100% on the webservers when exporting an excel spreadsheet with a lot of data in them, and more often than not, cause a timeout on the ASPX page. So I don't like to use them anymore.

My essential toolkit consists of:

Recommended websites: