Home > Back-end >  Understanding singleton use for global variable use C#
Understanding singleton use for global variable use C#

Time:07-17

It was suggested to me to use a singleton in my application and I am wondering if this is the correct approach.

I have a windows gaming handled tool app I am developing. It consists of two windows. A main window and a quick access menu window. These windows share similar components, like on both windows I have sliders to adjust screen brightness, volume, cpu TDP, etc. Both windows should show the same values.

Currently I am using a static class on a separate thread to get these values. It loops through and gets these values every few seconds via dispatcher timer. It is important to note that values like TDP require an external program that reads CPU MSR or MMIO values, so two threads should not concurrently be calling these read routines, as it can cause the external program to crash. These values then get stored into a static class in the main window housing the "global" variables.

  public static class GlobalVariables
    {
        //TDP global
        public static double readPL1 = 0;
        public static double readPL2 = 0;
        public static double setPL1 = 0;
        public static double setPL2 = 0;


        //brightness and volume setting
        public static int brightness = 0;
        public static int volume = 0;
    }

I always want this thread running as long as the application is running. I thought static would be appropriate since this isn't a scaled up app that would need dozens of this class running. I also might need to create events from this as well.

  1. Would a singleton with an initialization in both windows serve the same function?
  2. Would the stored variables stay consistent for both windows?
  3. Would using static routines cause an issue in my program (something that isn't scaled up)?
  4. One last question: if I go the singleton route and I want this code running separately from the UI should I initialize the class on a newly created thread in the window?

Thanks in advance!

CodePudding user response:

  1. Would a singleton with an initialization in both windows serve the same function?

A correctly implemented Singleton is initialized the first time it is accessed in an Application session and retains its value for the duration of the session.

  1. Would the stored variables stay consistent for both windows?

Wherever you access it in the code (from different Windows and other types that can be from any assembly), you will always get the same value.

  1. Would using static routines cause an issue in my program (something that isn't scaled up)?

Too vague question. The answer is from the details of the task, the chosen method of implementation.
Potentially, static implementations (including Singleton) have security issues, as static members have global access and can be intentionally malformed.
Therefore, only constants and methods are usually made static.
But such security in WPF is actually quite ephemeral.
With the "standard" implementation, it is still possible to get any Window, any of its elements from the Resources, Visual and Logical Trees.

In terms of scalability, there could be some issues if you need to add multiple View instances in the future. Then each of them may require its own instance with data, and not a static one with the same data for all.

  1. One last question: if I go the singleton route and I want this code running separately from the UI should I initialize the class on a newly created thread in the window?

If there are no thread-dependent elements in the code (it usually only UI elements), then the initialization flow usually does not matter.
If the initialization is long, then in order not to lag the GUI, it is better to do it asynchronously.

  • Related