{Log in or Register}

Enums in C# - use sbyte

August 14th, 2008 No Comments   Posted in C#

C# Code Specification:

The following example declares an enum type named Alignment with an underlying type of sbyte.

enum Alignment: sbyte
{
Left = -1,
Center = 0,
Right = 1
}

When you use enumerations in C# - you easy can declare new values as sbyte. Standart type of variable is int, but you can use and sbyte type.

C# - start only one copy of the program

August 10th, 2008 No Comments   Posted in C#

If you need that the program was started an only to one copy - can take advantage of such simple and useful class:

public class SingleInstance
{
private bool firstInstance = false;

public bool FirstInstance
{
get { return firstInstance; }
}

public SingleInstance()
{
Mutex mutex=null;
try
{
mutex = Mutex.OpenExisting("ProgramName");
}
catch (WaitHandleCannotBeOpenedException e)
{
firstInstance = true;
}

if (mutex == null)
{
mutex = new Mutex(false, "ProgramName");

GC.KeepAlive(mutex);
}
}
}

Now, we need edit Program.cs like here:

[STAThread]
static void Main()
{
SingleInstance single = new SingleInstance();

if (single.FirstInstance)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}

Now the program will be started only in one copy. A code works on the different versions of Windows - including Vista.


Tags: , ,