Showing posts with label Main. Show all posts
Showing posts with label Main. Show all posts

November 14, 2019

Console Application Template

Here is a simple template for console applications, including how to change the font color within the console

class Program
{
  static void Main(string[] args)
  {
    Console.WriteLine("Started SomeConsoleApplication");
    Console.WriteLine("");

    Console.ForegroundColor = ConsoleColor.Green; // Go/all is good colour
    try
    {
      // Do your console app work here
      SqlVersionFinder sqlVersionFinder = new SqlVersionFinder();
      var res = sqlVersionFinder.GetSqlVersion();
      Console.WriteLine("Version:   " + res.version);
      Console.WriteLine("Date:      " + res.date);
      Console.WriteLine("Copyright: " + res.copyright);
      Console.WriteLine("Edition:   " + res.edition);

    }
    catch (Exception ex)
    {
      Console.WriteLine("Oops, an exception occurred trying to run the application: "
                                  + ex.ToString());
      Console.ForegroundColor = ConsoleColor.Red; // Error colour
      Console.ResetColor();
    }

    Console.ResetColor();
    // This will stop the console application from finishing until a key is pressed
    // However you might not want this if the application is to be run from another
    // process or you might want to use a command line argument to enable or 
    // disable this option
    Console.WriteLine("");
    Console.WriteLine("Press any key to finish ...");
    Console.ReadKey(); 
  }
}

May 12, 2010

Creating A Single Instance WPF Application

Look here: Initial idea. - Did not seem to work!
How can I provide my own Main() method in my WPF application?
and here

Simplest solution found:
  1. On App.xaml build properties, set "Build Action" to Page.
  2. Add following code to "App.xml.cs" or equivalent, the "App" class source.
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows;

...

[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);

/// <summary>
/// Application Entry Point.
/// </summary>
[System.STAThreadAttribute()]
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
public static void Main()
{
  // Use mutex to ensure only single instance is running
  bool mutexOwnershipGranted = true;
  using (Mutex mutex = new Mutex(
    true, 
    "$SOMEUNIQUESTRING$", // Use GUID or string id here
    out mutexOwnershipGranted))
  {
    // If this is the only running instance
    if (mutexOwnershipGranted) 
    { // Then run app 
      DevHelperWpf.App app = new DevHelperWpf.App();
      app.InitializeComponent();
      app.Run();
    }
    else 
    { // Bring current running instance to the front
      Process current = Process.GetCurrentProcess();
      foreach (Process process in Process.GetProcessesByName(
          current.ProcessName))
      {
         if (process.Id != current.Id)
         {
            SetForegroundWindow(process.MainWindowHandle);
            break;
         }
      }
    }
  }
}