0

There is a Windows.Forms.Timer in my project. In the Timer.Tick Method Handler I create an instance of Manager class (My Own Class) And In Manager Constructor I create some threads and store them in a dictionary. The dictionary located in a class named TransManager which implemented with singleton pattern.

public class TransManager {
private static volatile TransManager _Instance;
 public static TransManager Instance
 {
     get 
     {
         lock (syncRoot)
         {
             if (_Instance == null)
                 _Instance = new TransManager();
         }
         return _Instance;
     }
 }

}

I implemented the class TransManager because I need to have all created threads which produced from different instance of Manager class in same place.

The problem is when a new instance of Manager adds threads in the dictionary the last threads are gone!

Note: When I create All threads within an instance of Manager class then all thread can share the dictionary safe. According to this can I say it is possible to singleton across threads?

I checked; There is no Dictionary.Clear() in my code!

I hope the problem was clear! Ask me if it is not.

Thank you.

Meysam Tolouee
  • 569
  • 3
  • 17
  • Erm each instance of your class has it's own dictionary. It's a singleton within the thread not across them. You'd need your singletonS to share some common 'service' to do something like that. – Tony Hopkinson Sep 08 '14 at 09:01
  • Can you guide me to a lead? @TonyHopkinson – Meysam Tolouee Sep 08 '14 at 09:22
  • Please see note in my edit. @TonyHopkinson – Meysam Tolouee Sep 08 '14 at 09:30
  • This is going down the singleton with constructor hole. See http://stackoverflow.com/questions/4203634/singleton-with-parameters Personally I'be be looking at not having a singleton. Take a step back and think about the when and what of your threads. – Tony Hopkinson Sep 08 '14 at 21:20

0 Answers0