473,378 Members | 1,140 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

singleton

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
Apr 22 '07 #1
10 1422
Nick,

Here is a thread that shows how to do it. It involves deriving from
WindowsFormsApplicationBase (located in the Microsoft.VisualBasic namespace,
in Microsoft.VisualBasic.dll):

http://groups.google.com/group/micro...8825edd7afbc60

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Nick C" <be****@gmail.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
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

Apr 22 '07 #2
http://msdn2.microsoft.com/en-us/library/ms998558.aspx

That link has a very good example.

daniel #

"Nick C" <be****@gmail.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
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

Apr 22 '07 #3
This looks like the answer

http://www.codeproject.com/csharp/singleinstance.asp
"Nick C" <be****@gmail.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
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
Apr 23 '07 #4

"Nick C" <be****@gmail.coma écrit dans le message de news:
%2****************@TK2MSFTNGP04.phx.gbl...
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() {
}
}
Apr 23 '07 #5
daniel # <da******@arrobaviabcp.comwrote:
http://msdn2.microsoft.com/en-us/library/ms998558.aspx

That link has a very good example.
Unfortunately a singleton isn't really what the OP is after - at least
not in the normal sense. He's trying to prevent multiple instances of
the *application* running, which isn't quite the same thing.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 23 '07 #6
Nick C <be****@gmail.comwrote:
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
See http://pobox.com/~skeet/csharp/faq/#...ation.instance

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 23 '07 #7
Oliver Osswald <ne****************@osswald.comwrote:
I would like to implement a singleton in .net.

Maybe like this (not tested):
Returns one, and only one, instance at a time....
I don't believe the OP actually wants a singleton, but even if he did,
the code you've provided isn't thread-safe. See
http://pobox.com/~skeet/csharp/singleton.html for an article on the
matter.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 23 '07 #8
"Jon Skeet [C# MVP]" <sk***@pobox.coma écrit dans le message de
news: MP************************@msnews.microsoft.com...
... See http://pobox.com/~skeet/csharp/singleton.html
for an article on the matter.
Thanks!
Apr 23 '07 #9
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).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
Nick C <be****@gmail.comwrote:
>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

See http://pobox.com/~skeet/csharp/faq/#...ation.instance

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Apr 23 '07 #10
On Apr 23, 1:18 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
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

Apr 23 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Tim Clacy | last post by:
Is there such a thing as a Singleton template that actually saves programming effort? Is it possible to actually use a template to make an arbitrary class a singleton without having to: a)...
10
by: E. Robert Tisdale | last post by:
Could somebody please help me with the definition of a singleton? > cat singleton.cc class { private: // representation int A; int B; public: //functions
1
by: Jim Strathmeyer | last post by:
So I'm trying to implement a singleton template class, but I'm getting a confusing 'undefined reference' when it tries to link. Here's the code and g++'s output. Any help? // singleton.h ...
3
by: Alicia Roberts | last post by:
Hello everyone, I have been researching the Singleton Pattern. Since the singleton pattern uses a private constructor which in turn reduces extendability, if you make the Singleton Polymorphic...
7
by: Ethan | last post by:
Hi, I have a class defined as a "Singleton" (Design Pattern). The codes are attached below. My questions are: 1. Does it has mem leak? If no, when did the destructor called? If yes, how can I...
3
by: Harry | last post by:
Hi ppl I have a doubt on singleton class. I am writing a program below class singleton { private: singleton(){}; public: //way 1
5
by: Pelle Beckman | last post by:
Hi, I've done some progress in writing a rather simple singleton template. However, I need a smart way to pass constructor arguments via the template. I've been suggested reading "Modern C++...
6
by: Manuel | last post by:
Consider the classic singleton (from Thinking in C++): ----------------------------------------------------- //: C10:SingletonPattern.cpp #include <iostream> using namespace std; class...
3
weaknessforcats
by: weaknessforcats | last post by:
Design Pattern: The Singleton Overview Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That...
3
by: stevewilliams2004 | last post by:
I am attempting to create a singleton, and was wondering if someone could give me a sanity check on the design - does it accomplish my constraints, and/or am I over complicating things. My design...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.