static events

  • Thread starter Thread starter parez
  • Start date Start date
P

parez

Hi,

I have implemented a static event. Is there anything special that i
have do? Is thread safety an issue?
 
Hi,

I have implemented a static event. Is there anything special that i
have do? Is thread safety an issue?

Its a winforms application .net 3.0
 
Its a winforms application .net  3.0

If I remember correctly, it is not thread safe, if you are using any
volitle resource I would suggest locking them.

lock(var)
{
}

Thanks.
 
Thread safety is as much an issue as it would be with an instance event.
The only difference is that with a static event you don't have the option
of dedicating an instance to each thread as a way of solving the thread
safety issues.

Otherwise, it's the same and you'd address the issues in the same way.

Pete

Thanks..

that means static and instance event in singleton would make no
difference?

TIA
 
Thanks..

that means  static and instance event in singleton would make no
difference?

TIA

Yes but if you are attempting to write a singleton pattern I would
recommend sticking to static for the method you are going to invoke to
get the singleton instance. That is the safest way of getting it.
 
Peter said:
With respect to thread safety? Yes, no difference.

No difference once the singleton is set up... creating the singleton only
once raises some new thread safety issues, especially if it is done lazily.
 
No difference once the singleton is set up... creating the singleton only
once raises some new thread safety issues, especially if it is done lazily..

yes I agree 100% on that. What I meant is the way how the objects
consume. If he doesn't use static but end up using instance, in a
completly different class the instance may not be available and
accidently he could create the instance again. May I am rambling..
please ignore me :)
 
Hi Ben,

What do you mean by "creating the singleton only once raises some new thread
safety issues , especially if it is done lazily."?

1) If done properly, isn't a singleton object supposed to be created only
once anyway?

2) If the singleton is not done in a lazy way but instead you put a lock on
the whole operation, what other thread safety issues can arise?

Thanks
 
Rene said:
Hi Ben,

What do you mean by "creating the singleton only once raises some new
thread safety issues , especially if it is done lazily."?

1) If done properly, isn't a singleton object supposed to be created
only once anyway?

Doing that in a threaded application is pretty challenging. If you use a
type initializer, .NET will do the synchronization for you, which is
typically the way to go. .NET may, but is not required to, run type
initializers lazily.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top