Archive for the ‘Other’ Category

Simple threading example in C# / CSharp

Friday, June 17th, 2011

Threading example – idea is that you can copy and paste this code in to get you started quickly.

using System;
using System.Threading;

class ThreadJobClass
  {
    public bool fetching = false;
    public bool fetched = false;
    public string value = null;

    protected int rand;

    public ThreadJobClass(int _rand)
    {
      rand = _rand;
    }

    public void run()
    {
      try
      {
        fetching = true;
        Console.WriteLine("Fetching from " + rand.ToString());
        Thread.Sleep(rand);
        value = rand.ToString();
      }
      catch (Exception e)
      {
        Console.WriteLine(e);
      }
      finally
      {
        fetching = false;
        fetched = true;
      }
    }
  }

  class Program
  {
    static void Main(string[] args)
    {
      var insts = new List();
      var my_random = new Random();
      for (var i = 0; i < 5; i++)
      {
        var rand = my_random.Next(2000);
        var inst = new ThreadJobClass(rand);
        insts.Add(inst);
      }
      var num_fetched = 0;
      var num_fetching = 0;
      var max_fetching = 2;
      while (num_fetched < insts.Count)
      {
        foreach (var inst in insts)
        {
          if (num_fetching == max_fetching) break;
          if (inst.fetching || inst.fetched) continue;
          var ts = new ThreadStart(inst.run);
          var thread = new Thread(ts);
          thread.Start();
          num_fetching++;
          break;
        }
        num_fetched = 0;
        num_fetching = 0;
        foreach (var inst in insts)
        {
          if (inst.fetching) num_fetching++;
          if (inst.fetched) num_fetched++;
        }
        Thread.Sleep(150);
      }
      Console.WriteLine("Done");
      foreach (var inst in insts)
      {
        Console.WriteLine(inst.value);
      }
    }
  }

Installing .NET 4 – Error 1719

Tuesday, March 1st, 2011

Was having a lot of trouble installing .NET 4 on to Windows 7 Home Premium 64 SP1

The solution was:

regedit

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\MSIServer

Change WOW64 from 1 to 0

cmd

%windir%\system32\msiexec.exe /unregister
%windir%\syswow64\msiexec.exe /unregister
%windir%\system32\msiexec.exe /regserver

And then it was fine :-)

My error log looked like this

MSI (s) (DC:FC) [20:37:13:023]: Invoking remote custom action. DLL: C:\Windows\Installer\MSICE11.tmp, Entrypoint: SchedSecureObjects
MSI (s) (DC:B8) [20:37:13:024]: Generating random cookie.
MSI (s) (DC:B8) [20:37:13:051]: Created Custom Action Server with PID 1884 (0x75C).
MSI (s) (DC:98) [20:37:13:092]: Running as a service.
MSI (s) (DC:98) [20:37:13:094]: Custom Action Server rejected – Wrong Context
MSI (s) (DC:B8) [20:37:13:097]: CA Server Process has terminated.
Action start 20:37:13: SchedSecureObjects_x64.
MSI (s) (DC:94) [20:37:13:098]: Note: 1: 1719
CustomAction SchedSecureObjects_x64 returned actual error code 1601 (note this may not be 100% accurate if translation happened inside sandbox)
MSI (s) (DC:94) [20:37:13:738]: Product: Microsoft .NET Framework 4 Client Profile — Error 1719. The Windows Installer Service could not be accessed. This can occur if the Windows Installer is not correctly installed. Contact your support personnel for assistance.

Error 1719. The Windows Installer Service could not be accessed. This can occur if the Windows Installer is not correctly installed. Contact your support

Cloning arrays in python

Sunday, April 11th, 2010

For single dimensions arrays:

a2 = a[:]
non-scalers within array will be copied as references. Does the same thing as copy.copy()

For multi dimension arrays:

import copy
a2 = copy.deepcopy(a)
non-scalers within array will be cloned as new objects

Python print_r or var_dump

Friday, January 22nd, 2010

If you’re coming from PHP, you may be wondering how to do a print_r or var_dump.

import pprint
pprint.pprint(myobj)

Fix loud or stiff space-bar Microsoft Ergonomic Keyboard 4000

Sunday, December 27th, 2009

The Microsoft Natural Ergonomic Keyboard 4000 is an excellent product, no other keyboard really comes close to it for touch typing in my opinion.

However there can be one very annoying bit – the spacebar.  It can be excessively loud and/or stiff to press.  Fortunately though you can easily fix it yourself!

Loudness:

The problem is that the bottom of the plastic space bar connects with some plastic on the keyboard base.  To fix this you’ll want to add some cushioning to the base of the keyboard which you’ll find easy to do yourself.

First of all, you need to pull the spacebar off the keyboard, which is easiest if you pull from the top.  You’ll need to use a bit of force, though don’t worry your keyboard will be ok :-) .  Once the space bar is off you can put in your cushioning.  The image below shows where you’ll be attaching your cushions.

base-bits

I made my cushions from inexpensive heat-shrink tubing filled with some cotton wool and attached it to the keyboard with some double-sided tape, though you could easily substitute this with whatever else you have around, just as long as it is nice and squishy.  The image below show what it should it looks likes with one of the cushions.

cushion

Once the cushioning is in, remount the space bar and you should be good to go!

Stiffness:

The space-bar can sometimes be stiff to press, particularly on its side-edges.  The stiffness will usually go away after a period of normal use, though you can speed this natural process up a bit by simply tapping it a couple of thousand times where your thumbs normally press it.  For me this spot is underneath the ‘V’ key on the left and about halfway across the ‘N’ key on the right.  Just sit there and tap these spots really fast for several minutes.  You can also try pressing with very little pressure in order to get the key to stick, and then apply more pressure so that it rubs really hard inside the keyboard.  Press on the top corners of the space bar to get even more resistance.

If this doesn’t work, this you’ve basically got a dud that will only frustrate you, so return it to the shop and get another one!

Perl in_array()

Thursday, December 17th, 2009

Perl can use the grep function as the equivalent of PHP’s in_array() function.  Essentially

$found = grep $_ eq $my_value, @my_array

Note, the grep() function is world unto its own, it can do quite a bit more than just this.  $found is actually $my_value if it finds it, which tends to evaluate to ‘True’ in Perl.

Python in_array()

Thursday, December 17th, 2009

Python uses the “in” statement as the equivalent to PHP’s in_array()

my_boolean = my_value in my_list

You can view more PHP-Python statements here.

Python str_replace()

Thursday, December 17th, 2009

Python uses a function on the str/unicode object instead of a standalone function

new_s = s.replace(match, replacement)

You can view more PHP-Python statements here.

Python sprintf()

Thursday, December 17th, 2009

Python uses the % operator to do a sprintf() function.

“The %s brown %s” % (“quick”, “fox”)

Brackets can be omitted if there for single arguments

“The %s brown fox” % “fast”

You can view more PHP-Python statements here.

Python error/exception handling

Wednesday, December 9th, 2009

This code quickly shows how to do basic error handling in python

try:
  # None + 1
  raise ZeroDivisionError
except ZeroDivisionError:
  print 'You cannot divide by zero!'
except:
  print sys.exc_info()[0]
else:
 print 'No errors raised'
finally:
 print 'Run Clean up actions'