March 27, 2006

C# Xml Creation Sample

Parsing Hexadecimal

How to parse a number in Hexadecimal form
 newByte = .Parse(hex, System.Globalization.NumberStyles.HexNumber);

C# OpenPGP Encryption

C# OpenPGP Encryption Imlementation - Could be useful

Dispose Pattern

Explains how to implement IEnumerator and Contains basic IDispose pattern and more Msdn Guidelines on Implementing IDisposable and Equals Operators

Basic IDisposable Implementation
// Design pattern for a base class.
public class Base: IDisposable
{

#region Dispose Implementation

   // Use C# destructor syntax for finalization code.
   ~Base()
   {
      // Simply call Dispose(false).
      Dispose (false);
      Debug.Assert(false, "This object was not disposed of");
   }

   private bool _isDisposed;

   //Implement IDisposable.
   public void Dispose() 
   {
      Dispose(true);
      GC.SuppressFinalize(this); 
   }

   protected virtual void Dispose(bool disposing) 
   {
      if (_isDisposed)
        return;

      if (disposing) 
      {
         // Free other state (managed objects).
      }
      // Free your own state (unmanaged objects).
      // Set large fields to null.
      _isDisposed = true;
   }

#endregion Dispose Implementation


...
}
   
// Design pattern for a derived class.
public class Derived: Base
{  

#region Dispose Implementation 

   private bool _isDerivedDisposed = false;

   protected override void Dispose(bool disposing) 
   {
      if (disposing) 
      {
         // Release managed resources.
      }
      // Release unmanaged resources.
      // Set large fields to null.
      
      // Call Dispose on your base class.
      base.Dispose(disposing);

      _isDerivedDisposed = true;
   }
   
   // The derived class does not have a Finalize method
   // or a Dispose method with parameters because it inherits
   // them from the base class.

#endregion Dispose Implementation

}

Safely Parse An Ip Address

This helper method safely parses an IP address from a string using the System.Net.IPAddress class
private static string SafeIpAddressFromString(string value)
{
  if (ReferenceEquals(value, null))
      value = ""; // System.Net.IPAddress.Parse cannot handle null values
  System.Net.IPAddress ipAddr = System.Net.IPAddress.Parse(value);
  return ipAddr.ToString();
}

March 16, 2006

Installing Windows Services

Using InstallUtil Install: InstallUtil.exe algo.exe Uninstall: InstallUtil.exe /u algo.exe Batch File To Install A service
C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\InstallUtil.exe 
/showcallstack C:\...\bin\XXXService.exe pause
Batch File To Uninstall A service
C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\InstallUtil.exe /u
/showcallstack C:\...\bin\XXXService.exe pause
Programmatically Add a reference to 'System.Configuration.Install' Install:
String []installParams= {filename};
System.Configuration.Install.ManagedInstallerClass.InstallHelper(installParams);
Uninstall:
String []installParams= {"/u", filename};
System.Configuration.Install.ManagedInstallerClass.InstallHelper(installParams);

Manual Locking With Monitors

When we want to use a timed 'lock' we can not use the normal lock statement, we have to use the monitor directly.
private object someLock = new object();
...
// Here we need to execute something within a Timeout period
// but we could exceed the timeout just waiting for the lock on our object!
algo = null;
int start = System.Environment.TickCount;
bool lockAquired = Monitor.TryEnter(someLock, timeout);
if (lockAquired)
{
  try
  {
    int elapsedTime = (System.Environment.TickCount-start);
    // Did we use any time up waiting for the lock? Take it off the original timeout 
    // new timeout = original timeout - time expired obtaining the lock
    int timeLeft = timeout - elapsedTime;
    if (timeLeft > 0)
    {
        DoSomething(timeLeft, out algo);
    }
  }    
  finally
  {
    Monitor.Exit(someLock);
  }
}

March 15, 2006

Free Programmers Resources and Tools

ASP.NET 2.0 Links