Posts

Showing posts from May, 2009

C# Indexer example

publicclass SampleIndexer
{
privatereadonly Dictionary<string,Dictionary<string, string>> list = new Dictionary<string,Dictionary<string, string>>();

public SampleIndexer()
{
list.Add("One",new Dictionary<string, string>());
list.Add("Two", new Dictionary<string, string>());
}

publicvoid Add(string name, string key, stringvalue)
{
list[name].Add(key,value);
}

public Dictionary<string,string> this[string name]
{
get
{
return list[name];
}
}

publicstringthis[string name, string key]
{
get
{
return list[name][key];
}
}
}
[TestFixture]
publicclass TestIndexer{

[Test]
publicvoid Test()
{
SampleIndexer indexer = new SampleIndexer();
indexer.Add("One","key1","value1");
indexer.Add("Two","key2","value2");

Console.WriteLine(indexer["One"]["key1…

C# String.Format("{0}", "formatting string")

C# Latch implementation

This is a c# Latch implementation for multithreaded applications (equivalent to java CountDownLatch).It’s used in a situation when you have a one thread waiting for a number of other threads to finish. The Test below also uses a Semaphore to run only a specific number of child threads at one time.publicclass Latch
{
privatereadonlyobject locker = newobject();
privateint count;

public Latch(int noThreads)
{
lock (locker)
{
count = noThreads;
}
}

publicvoid Await()
{
lock (locker)
{
while (count > 0)
{
Monitor.Wait(locker);
}
}
}

publicvoid CountDown()
{
lock (locker)
{
if (--count <= 0)
{
Monitor.PulseAll(locker);
}
}
}

publicint GetCount()
{
lock (locker)
{
return count;
}
}
}
[TestFixture]
publicclass TestLatch
{
static Latch latch = new Latch(5);
private Random random = new Random();
static Semaphore semaphore = new Semaphore(3,3);

publ…

ClickOnce Expired Certification Solution

RenewCert is a solution to Microsoft VS2005 ClickOnce deployment certification problem when it expires after one year. More info about the problem can be found on Microsoft website http://support.microsoft.com/kb/925521