Classes on library

  • WinAPI (Contain all Win API Calls) NAMESPACE
    • Dbghelp (All API calls for Dbghelp.dll) STATIC CLASS
    • Kernel32 (All API calls for Kernel32.dll) STATIC CLASS
  • About (Store some constants about library, author, webpage, etc...) STATIC CLASS
  • AssemblyEx (Util functions from assembly) STATIC CLASS
  • ExceptionHandler (Automatic handles and log unhandled exceptions) CLASS
  • Instance (Make your application single instance) CLASS
  • MemoryManagement (Automatic memory management) CLASS
  • MiniDump (Used to create mini dumps) STATIC CLASS
  • Profiling (Used to profiling your functions, Dictionary of ProfilingData) CLASS
    • ProfilingData (Class that hold the profiling data, StartDate, EndDate, TimeTaken, ...) CLASS
  • SystemInformationEx (Provide some GET functions related to system information) STATIC CLASS
  • VistaSecurity (Functions relative to Windows Vista and/or above) STATIC CLASS

The following examples will use Windows Application Form

First Note

Most of that classes should be used outside scope and declared as static.
I'm going use public static to access to that classes anywhere.
Classes that have an Start(); also have an Stop();

Instance Class

public static Instance instance;
[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    #region Instance Class (Make single instance app)
    // Check if that is a single instance, and name mutex as: "{ProductName}Mutex"
    instance = new Instance(Application.ProductName);
    // instance is created but make sure you call next line:
    // First parameter is used to define mutex level (Global or this session only)
    // Second parameter is used to check processes equality after check mutex
    if (!instance.MakeSingleInstance(true, false))
    {
        Instance.ShowDuplicateInstanceDialogError();
        // Stop and quit App.
        return;
    }
    #endregion

    Application.Run(new MainFrm());
    // Note: by calling 'Instance.Dispose' method will release mutex,
    // and app will be multi instance,
    // But still checks for process equality!
    // instance.Dispose();
}

MemoryManagement Class

public static MemoryManagement memory;
[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    #region Memory Management class
    // MemoryManagement only make effect in WinNT Systems, 
    // if your app is multi system use that.
    if (MemoryManagement.CanUseClass())
    {
        memory = new MemoryManagement();
        memory.Start();
    }
    else
    {
        memory = null;
    }
    #endregion

    Application.Run(new MainFrm());
}

ExceptionHandler Class

Note: Please be sure you start before run your application.
If you not do so please use the following code before Run Application

Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

public static ExceptionHandler exceptionHandler;
[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    #region Exception Handler Class (Automatic cath and log unhandled exceptions)
    // Automatic cath and log unhandled exceptions
    exceptionHandler = new ExceptionHandler();
    // Next two lines are optional
    exceptionHandler.PrefixText = "This file represets an error cath on our program";
    exceptionHandler.SuffixText = 
         "Please fell free to send us that errors files via email: myapp@company.com";
    exceptionHandler.StartHandlingExceptions();
    #endregion

    Application.Run(new MainFrm());
}

Profiling Class

Note: Is always better you work directly with the Profiling.ProfilingData
Profiling Class is a Profiling.ProfilingData Dictionary

public static Profiling profiling;
[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    #region Profiling Class
    // Our profiling will determinate the time taken by the app to initialize.
    profiling = new Profiling();
    Profiling.ProfilingData profilingData = profiling.Create("InitApp", true);
    #endregion

    // Wait 1.5s and see the output
    Thread.Sleep(1500);

    #region End started profile and show results
    profilingData.Stop();
    MessageBox.Show(
        string.Format(
            "Start Date: {0}\nEnd Date: {1}\nTime Taken: {2}ms", 
                 profilingData.StartDate,
                 profilingData.EndDate,
                 profilingData.TimeTaken.TotalMilliseconds
            ),
        "Our application initializes time");
    #endregion

    Application.Run(new MainFrm());
}

Other Example

/// <summary>
/// Do some job and returns the time it takes
/// </summary>
Profiling.ProfilingData MyFunction()
{
    // Note is better use a single class to all your profiling, but...
    // Initialize class, create a profiling and start it
    Profiling profiling = new Profiling("MyFunction", true);
    // Also can use:
    // Profiling.ProfilingData pData = profiling.Create("MyFunction");
    // if (pData == null)
    //     Console.WriteLine("Won't execute profiling, name already exists.");
    
    // Lets make some garbage
    for(int i = 1; i <= 10000; i++)
    {
        Random rand = new Random();
        Console.WriteLine(rand.Next(0, i));
    }
    
    // Function ends now let stop profiling and return the results.
    Profiling.ProfilingData pData = profiling.Get("MyFunction");
    // Note: you can check if pData is null if you not sure that exists
    pData.Stop(); // profiling.Stop("MyFunction"); also works
    return pData;
}

Profiling.ProfilingData pData = MyFunction();
if(pData.IsConcluded)
{
    Console.WriteLine("Total time to execute 'MyFunction': {0} ({1}ms)", 
                pData.TimeTaken, 
                pData.TimeTaken.TotalMilliseconds);
}
else
{
    Console.WriteLine("Developer doesn't write the function correct");
    Console.WriteLine("A ProfilingData must be Stopped, before use EndDate and TimeTaken");
}

Note: To avoid duplicated keys and fast codding you can use just Profiling.ProfilingData
Again: the Profiling class is optional is just a wrapper of Profiling.ProfilingData Class that holds a Dictionary<string, Profiling.ProfilingData>
But: if you want keep and store a Profiling function to use latter or get his results, is better use Profiling Class

Profiling.ProfilingData pData = new Profiling.ProfilingData();
pData.Start();

// Do your job here, time is counting!!!
// This way is more simple than use Profiling Class

pData.Stop();
/*
*
* pData.StartDate;
* pDate.EndDate;
* pDate.TimeTaken;
*
*/
// Discard variable
pData = null;

VistaSecurity Class

/// <summary>
/// Under Windows Vista, if current user is not an administrator, 
/// try elevate the permissions and exit from current application.
/// </summary>
if(!VistaSecurity.IsAdmin())
{
    if(VistaSecurity.RestartElevated(Application.ExecutablePath))
        Application.Exit();
}

Putting all together

#region Variables
public static Instance instance;
public static MemoryManagement memory;
public static Profiling profiling;
public static ExceptionHandler exceptionHandler;
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    #region Profiling Class
    // Our profiling will determinate the time taken by the app to initialize.
    profiling = new Profiling();
    Profiling.ProfilingData profilingData = profiling.Create("InitApp", true);
    #endregion

    #region Instance Class (Make single instance app)
    // Check if that is a single instance, and name mutex as: "{ProductName}Mutex"
    instance = new Instance(Application.ProductName);
    // instance is created but make sure you call next line:
    // First parameter is used to define mutex level (Global or this session only)
    // Second parameter is used to check processes equality after check mutex
    if (!instance.MakeSingleInstance(true, false))
    {
        Instance.ShowDuplicateInstanceDialogError();
        // Stop and quit App.
        return;
    }
    #endregion

    #region Memory Management class
    // MemoryManagement only make effect in WinNT Systems, 
    // if your app is multi system use that.
    if (MemoryManagement.CanUseClass())
    {
        memory = new MemoryManagement();
        memory.Start();
    }
    else
    {
        memory = null;
    }
    #endregion

    #region Exception Handler Class (Automatic cath and log unhandled exceptions)
    // Automatic cath and log unhandled exceptions
    exceptionHandler = new ExceptionHandler();
    // Next two lines are optional
    exceptionHandler.PrefixText = "This file represets an error cath on our program";
    exceptionHandler.SuffixText = 
         "Please fell free to send us that errors files via email: myapp@company.com";
    exceptionHandler.StartHandlingExceptions();
    #endregion

    #region End started profile and show results
    profilingData.Stop();
    MessageBox.Show(
        string.Format(
            "Start Date: {0}\nEnd Date: {1}\nTime Taken: {2}ms", 
                 profilingData.StartDate,
                 profilingData.EndDate,
                 profilingData.TimeTaken.TotalMilliseconds
            ),
        "Our application initializes time");
    #endregion
            
    Application.Run(new MainFrm());
    // Note: by calling 'Instance.Dispose' method will release mutex,
    // and app will be multi instance,
    // But still checks for process equality!
    // instance.Dispose();
}

Last edited Feb 18, 2010 at 4:19 PM by sn4k3, version 7

Comments

No comments yet.