|
By why do we need the "instance" in a singleton at all? Making the methods static seems to be enough:
<br />
public sealed class SingletonNoInstance<br />
{<br />
private static Object m_objInit = new InitObj();<br />
<br />
private static Object InitObj()<br />
{<br />
return new Object(); <br />
}<br />
<br />
public static void ThreadSafeDoSomething() <br />
{ <br />
lock(typeof(SingletonNoInstance))<br />
{<br />
}<br />
}<br />
}<br />
The private member m_objInit and method InitObj() can be removed if no initialization of the class is required.
|
|
|
|
|
There's a difference between singletons and a class with static methods. If you just have simple static methods (like the System.Math class), you don't need to construct a singleton. Singletons are useful when you want lots of different callers to use the same resource without having multiple instances of that resource (or the class that has that resource). For instance, all throughout the .NET BCL (and our application I architected), most classes in an assembly use a internal singleton that has a single instance of a ResourceManager that contains strings (and other resources at times) for all classes to use (like common exception messages or property categories and descriptions). It would be horribly inefficient to have an instance of this ResourceManager for each class (and who knows how many instances of each class?!), so they use a singleton with a single instance of the ResourceManager .
There are many other reasons for singletons (if you know anything about remoting, there's some good examples for that, too). This is just a common example I think of because I use it a lot and it is far more efficient to do it this way that for each instance of a class to have it's own - especially when you're sharing string resources amongst other classes.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
In doing further reading, I came upon Jon Skeet's excellent article.
http://www.yoda.arachsys.com/csharp/singleton.html
Unless I'm reading him incorrectly, the fourth version of the Singleton he presents is the same code I have been using.
public sealed class Singleton<br />
{<br />
private Singleton(){}<br />
public static readonly Singleton Instance = new Singleton();<br />
}
He calls it "thread-safe without using locks". What I understand from his article is that this thread-safety is due to the way the Framework handles static type initializers. Have I missed something?
|
|
|
|
|
You'll also notice he calls it "lazy". Besides, you still need to add the static constructor so the correct IL is generated.
The biggest difference between this and the way we were previously talking about (double-checked locking) is that static constructors as executed at most once per application domain. This means that your Singleton will be a different singleton in each AppDomain your application uses (in a distributed app that uses Remoting or Web Services, there is at least two AppDomain , and even a your process can create additional AppDomain on the same machine).
One important thing to keep in mind - the performance penalties for the double-checked locking are negligible - it only locks once and the first conditional statement is a one-line instruction in IL and is extremely quick. This is the proper way to do it. Lazy programming leads to bugs and holes.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Very cool.
The clouds have parted.
I will not be traversing AppDomains but I will be accessing the singleton from a ServerContextSink.
I don't know whether that would also defeat the thread-safety of the first method, but as you say the performance hit for the lock is negligible (certainly negligible compared to the 1000% hit I seem to be taking instantiating ContextBoundObject), I will definite use double-checked locking.
Robert Zurer
robert@zurer.com
|
|
|
|
|
Hi guys...
I've created a setup project using VS.NET. It works fine, change some registry keys, create my database (using my installer class), creates some IIS virtuals dirs. The problem is that I need to force the machine to reboot after the installation has been completed. Does anyone know how to to this ? Is there any parameter I can change in my installer class to do this ?
thanks in advance
Mauricio Ritter - Brazil
Sonorking now: 100.13560 MRitter
English is not my native language so, if you find any spelling erros in my posts, please let me know.
|
|
|
|
|
I just covered this a day or two ago (searching first is always a good idea). Set the REBOOT property (must be all upper-case) to Force. See the Windows Installer SDK for more information.
Since VS.NET's setup projects suck, you'll have to download Orca (part of the Windows Installer SDK, which is part of the Platform SDK) and make the changes post-build. Just open the MSI package, find the Properties table, and add said property. You could also do this using a custom action, but it's quite a bit of overhead for a such a simple task.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Heath Stewart wrote:
You could also do this using a custom action, but it's quite a bit of overhead for a such a simple task.
Actually I already have a installer class with custom actions in my setup project, it would be easier to insert the property there, I just don't know where to insert it. I tryed the Context.Parameters collection but it didn't work. Any ideias ?
Mauricio Ritter - Brazil
Sonorking now: 100.13560 MRitter
English is not my native language so, if you find any spelling erros in my posts, please let me know.
|
|
|
|
|
You can't, actually. .NET Installer classes have nothing to do with Windows Installer, Windows Installer is just able to run them as custom installer actions. I was talking about native custom actions that you'd have to write in C/C++ (with C-style functions).
The easiest way is to install Orca and edit the MSI yourself. Just go to mdsn.microsoft.com and go to downloads. Click Platform SDK but only install the Windows Installer SDK. It will install only a few required components from the PSDK itself. Legally, I cannot just send you the installer for Orca but it is an invaluable tool and is worth the download if you're messing with installer packages.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
C++ has its STL for doing stuff like linked lists etc.
What about .NET? I need data structures to dynamically manage lots of different objects. Are the Collections fast enough?
Which types of what are found in .NET are suitable for real-time applications that need to create and destroy lots and lots of objects very fast? at the same time the garbage collector also should not interfere too much. Hopefully it will be unnoticable.
Any suggestions from your experience? What should I prefer.
|
|
|
|
|
Well an example could be: performance of stl::list vs. .NET collections?
Application: real-time simulator
Thank you!
|
|
|
|
|
The GC does not "interfere" unless your code calls GC.Collect or if memory usage grows too large (to help avoid this case - if your objects implement IDisposable - call Dipose() on your objects when removing them from the collection).
Most collections and lists in System.Collections are O(n). If this isn't suitable, implement ICollection or IList (you don't have to, but it makes it usable for a lot of code!) and make a linked-list yourself. It isn't hard, and there are several articles here on CP about it (not to mention all over the 'net since it's such a basic, academic problem).
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Heath Stewart wrote:
Most collections and lists in System.Collections are O(n).
Hehe, Heath, I have never known a static structure can be O(n), unless I have missed some really important lesson along the lines.
leppie::AllocCPArticle("Zee blog"); Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it.
|
|
|
|
|
I meant that most of the operations for collections and lists are O(n). I take common sense for granted.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
if(remoteServer.StartsWith("www")||remoteServer.StartsWith(!="http://"))
That's my if .
notice the part after the or || .
how can I make evaluate if remoteServer dosn't start with "http:// "?
So, basically how can I put a != into a if with no = in the first place... if you get what i'm sayin'.
Thanks.
/\ |_ E X E GG
|
|
|
|
|
if(remoteServer.StartsWith("www")||!remoteServer.StartsWith("http://"))
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Muchos gracias. Are these welcome messages necessary?
/\ |_ E X E GG
|
|
|
|
|
eggie5 wrote:
Are these welcome messages necessary?
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
|
No, I mean I have no idea what you're talking about. What "welcome messages"?
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
I was just trying to end the subject, because I realized I had made a typo. So, with that said. Thanks for your help.
/\ |_ E X E GG
|
|
|
|
|
Is there any way of preventing validation in windows forms when pressing the Close button (X) on the title bar
Dejan
|
|
|
|
|
Not sure about this but its worth a try.
Create an event handler for the Closing event (which gets fired just before a window is closed) and remove the validation.
--Colin Mackay--
"In the confrontation between the stream and the rock, the stream always wins - not through strength but perseverance." (H. Jackson Brown)
Enumerators in .NET: See how to customise foreach loops with C#
|
|
|
|
|
What do you mean "remove the validation"?
I tried to do
textbox1.Validating-=<br />
new System.ComponentModel.CancelEventHandler(this.textBox1_Validating);<br />
but it doesn't work.
I have a text box with a validating event handler attached to it and when I press the close button on the title bar, it activates the validation handler for the text box. I tried to set CausesValidation to false on the form, but Can I do that on the caption's close button?
Dejan
|
|
|
|
|
That code only works if an event handler was added for that method in the first place.
You could try handling Form.Closing and set a boolean field or something so that your validation event handlers don't validate if it's set. Unfortunately, I can't find any information about order of events in relation to Validating, Valiated, Closing, and Closed, but it's worth a shot. Another way - if the order is correct - would be to just remove the validating event handlers in the Closing event, so long as CancelEventArgs.Cancel (passed to the event handler) is not set to true . It might be even better that you override OnClosing and call the base.OnClosing method to make sure that all event listeners have had a chance to set CancelEventArgs.Cancel so you know for sure if you really should unhook those validation event handlers.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|