singleton

  • Thread starter Thread starter Nick C
  • Start date Start date
N

Nick C

Hi

I would like to implement a singleton in .net. The application only can be
run by one user at the time. Does anyone have any articles/samples that can
help me with this problem? thanks

N
 
Nick C said:
Hi

I would like to implement a singleton in .net.

Maybe like this (not tested):
Returns one, and only one, instance at a time....

public class Singleton {
private static Singleton single;
static private bool created = false;
//creates an instance privately
private Singleton() {
single = this;
created = true;
}
//always returns the same instance
public static Singleton getInstance() {
if (! created) {
single = new Singleton ();
}
return single;
}
//carries out some Operation
public void doSomething() {
}
}
 
Jon,

While the Mutex approach works, I am curious why you wouldn't use the
WindowsFormsApplicationBase class. It wraps up everything nice and neat for
you, as well as passes arguments from the new instance (which will be shut
down) to the running instance of the program.

Why reinvent the wheel? The only reason I can think of would be because
one doesn't want to load Microsoft.VisualBasic.dll (because of size, not
because it is Visual Basic, but the size argument isn't valid for most
applications that are being written).
 
While the Mutex approach works, I am curious why you wouldn't use the
WindowsFormsApplicationBase class. It wraps up everything nice and neat for
you, as well as passes arguments from the new instance (which will be shut
down) to the running instance of the program.

Why reinvent the wheel? The only reason I can think of would be because
one doesn't want to load Microsoft.VisualBasic.dll (because of size, not
because it is Visual Basic, but the size argument isn't valid for most
applications that are being written).

The principle reason it's not on the web page is that I didn't know
about it when the page was originally written. That leads to one
drawback of it - it's only available in .NET 2.0. I'll change the web
page (when I get some time!) to highlight it as a good solution for
2.0, and to also point to a page giving some code with similar
capabilities for 1.1. (Someone (Willy?) pointed it out in another
group recently. I'm just slightly backed up in terms of things I'm
meant to be doing atm...)

Jon
 
Back
Top