Category Archive: Programming

Subcategories: Bash  C#  Javascript  Php 

New programming slang

Source Linky

Some of my favourites:

Heisenbug
It is an error, which disappears or alters when it is attempted to be identified by analogy to Heisenberg uncertainty principle in quantum physics.

Reality 101 failure
The program (or more likely feature of a program) does exactly what was asked for, but when it’s deployed it turns out that the problem was misunderstood and the program is basically useless.

Protoduction
This is a prototype that ends up in production.

Megamoth
MEGA MOnolithic meTHod. Usually, it stretches over two screens in height and often contained inside aGod Object (an object that knows or does too much). Megamoths of greater size than 2k LOC have been sighted. Beware of the megamoth!

Easy php output compression

Below is what I regard as a reliable method of doing gzip output compression.

One thing that normally breaks gzip compression is something printing/echoing before the ob_start (usually the result of an error, debug output, errant space somewhere etc…), this is where the ob_get_length and ob_flush come in.

The problem is that headers_sent is not a reliable way to tell if any thing has been output because it may still be in the output buffer, so if we flush it before hand problem solved.

<?php

function doPage()
{
   echo 'Hello World';
}

if (ob_get_length() > 0)
   @ob_flush();

if (!headers_sent() && @ob_start("ob_gzhandler"))
{
   doPage();
   ob_end_flush();
   //Probably a good idea to exit here as more output would break it
   exit;
}
else
{
   doPage();
}

Threaded flicker-free control rendering in c#

I recently ran into a problem where a control was rendering slow enough that it caused flickering (as the screen region is cleared and then redrawn).

Looking around the ‘net I settled on rendering the control to a bitmap and then drawing the bitmap to the screen (fast operation), which is called double buffering.
(more…)

Text block clock

Clock

This is a clock that tells you the time by highlighting text in a block of text. In this case using a C# Console App. Not exactly original, but I thought I’d give it a try.
The progress bar counts the time until the next 5 minute mark.

Download Code
Download Executable
(more…)

Maximizing a print preview dialog in c#

Here’s a way to get a print preview dialog (or perhaps any resizeable dialog) maximised.

dlg.Show();
//Make full screen
//cant use WindowState as its not a form
//dlg.DesktopBounds = Screen.FromControl(dlg).WorkingArea;
((Form)dlg).WindowState = FormWindowState.Maximized;
dlg.BringToFront();

Although some times it disappears behind whatever form had focus last.

Update

Looks like the dialog can be cast to a form allowing WindowState to be set.

PrintPreviewDialog dlg = new PrintPreviewDialog();
dlg.Document = doc;
((Form)dlg).WindowState = FormWindowState.Maximized;
dlg.ShowDialog();

Detecting drive insertion and removal in C#

Here is some C# code to detect when a logical volume (e.g. USB Memory Stick) is inserted or removed via the WM_DEVICECHANGE message with WndProc.

However this doesn’t tell you what has been inserted/removed, you will have to poll the drives manually to find out.
If you need that you will have to register to receive events from windows which involves P/Invoking RegisterDeviceNotification (See Drive Detector at Code Project).

[...]
using System.Runtime.InteropServices;
using System.Security.Permissions;

public class MyForm : Form
{
        const int WM_DEVICECHANGE = 0x0219;
        const int DBT_DEVICEARRIVAL = 0x8000; // system detected a new device
        const int DBT_DEVICEREMOVECOMPLETE = 0x8004; //device was removed
        const int DBT_DEVNODES_CHANGED = 0x0007; //device changed
        const int DBT_DEVTYP_VOLUME = 0x00000002; // logical volume

        [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_DEVICECHANGE
                && m.WParam.ToInt32() == DBT_DEVICEARRIVAL
                || m.WParam.ToInt32() == DBT_DEVICEREMOVECOMPLETE
                || m.WParam.ToInt32() == DBT_DEVNODES_CHANGED))
            {
                if (m.WParam.ToInt32() != DBT_DEVNODES_CHANGED)
                {
                    int devType = Marshal.ReadInt32(m.LParam, 4);
                    if (devType == DBT_DEVTYP_VOLUME)
                    {
                        //Poll drives
                    }
                }
                else
                {
                    //Poll drives
                }
            }

            base.WndProc(ref m);
        }
}

Live Mirror

Here’s a C# program to mirror all changes in one directory with another.

I build it to let me edit a PHP web project on my development PC and have it mirrored to my development server, without any action on my part.

Its all being hosted at Google Code, go on give it a try :)
(more…)

Converting Videos for the Creative Zen

I have a Creative Zen, its a very nice little mp3/video player, and while it plays xVid/DivX/WMV its a bit picky about what it will play.

So ive created this script to convert videos to a format it will play (320×240, xVid, with auto black bars to maintiain aspect ratio & 128KB ABR MP3 audio).
Its a Bash script using Mplayer / Mencoder.

Files

Lambda's and Anonymous functions in C# 3.5

Here’s a lambda function in use with the new extensions for a List object.

personnel.foreach(x => x.DoSomeCoolStuff());

(more…)

360° Angle Class for C#

Here is a nifty class to handle an angle. it will wrap around increments and decrements that exceed 360 or 0, e.g. adding 20 to 350 will give 10 not 370 and subtracting 10 from 5 will give 355 not -5.

  • Implicit conversion to and from double
  • ToString and GetHashCode are mapped to the inner angle
  • Operators +,-,>,>=,<,<= have been implemented.
  • Mixing this class and doubles in statements works seemlessly.

Code sample:

Angle angle = 0;
double angle2 = angle + 5;
Console.WriteLine("a1:{0) a2:{1}",angle,angle2);
//Output: a1:0 a2:5

Files