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


Design pattern for a base class with unmanaged resources.

// Design pattern for a base class with unmanaged resources.
public class Base: IDisposable
{

#region Dispose Implementation

// Use C# 'destructor' style syntax for a finalizer.
// Finalizer is only required when the class has Unmanaged resources
// but can also be used to detect memory leaks
~Base() 
{
  // This is defensive code to ensure that even if you forget to call Dispose() unmanaged objects stil get freed
  Dispose (false); // Dispose of unmanaged resources 
  Debug.Assert(false, nameof(Base) + " with id of " + someId + " was not disposed of"); // Useful for detecting memory leaks
}

protected bool _isDisposed;

//Implement IDisposable.
public void Dispose() 
{
  Dispose(true); // Dispose of managed and unmanaged resources
  GC.SuppressFinalize(this); // Not required now
}

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

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

#region Dispose Implementation 

   protected override void Dispose(bool disposing) 
   {
      if (_isDisposed)
        return;
    
      if (disposing) 
      {
         // Free other state (managed objects).
      }
      
      // Release unmanaged resources here
      // Set large fields to null.
      
      // Call Dispose on your base class.
      base.Dispose(disposing);
   }
   
   // 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

}

Finally the simplest case

// Design pattern for a simple class with only small unmanaged resources.
public class SimplestDispose : IDisposable
{

#region Dispose Implementation

#region DEBUG
// Finalizer is only used to detect memory leaks
~SimplestDispose() 
{
  Debug.Assert(false, nameof(Base) + " with id of " + someId + " was not disposed of"); // Useful for detecting memory leaks
}
#endregion DEBUG

//Implement IDisposable.
public void Dispose() 
{
  // Dispose of managed resources here
  
#if DEBUG  
  GC.SuppressFinalize(this); // Not required now except for detecting memory leaks
#endif
}

#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