run once code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • USBZ0r
    New Member
    • Mar 2009
    • 20

    run once code

    hey guys, im still learning to code in C# im enjoying it i must say but i find it confusing sometimes!

    I was wondering how i would go about storing a string, basicly, when my program loads, (single form) it does a series of OS registry checks. now, some parts of my application requite the results for several things, but i dont want to have the actual "checks" run every time its needed, how can i make it run once then store the value for later use?

    Here is one of the checks i run, a check to see if its a 64 or 32bit OS. can i have this run once and store its resault and reuse it?


    Thanks!

    Code:
            private static bool is64BitOperatingSystem()
            {
                RegistryKey localEnvironment = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment");
                String processorArchitecture = (String)localEnvironment.GetValue("PROCESSOR_ARCHITECTURE");
                if (processorArchitecture.Equals("x86"))
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Notice that little used file called Program.cs ??

    Put your values in there, and they will be available throughout the scope of your program.

    Code:
        public static class Program
        {
            public static bool ShowSplashScreen = true;
            public static int SomeValue = -1;
            public static string SomeName = string.empty;
    Then any other class can reach them like this

    Code:
    string myName = Program.SomeName;

    Comment

    • EARNEST
      New Member
      • Feb 2010
      • 128

      #3
      Hm...I am not a guru, but what if u store the result as a string or as class var?

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        Hm...I am not a guru, but what if u store the result as a string or as class var?
        I don't think the OP was confused about the what data type to use, so much as where can he put it so any class within the application can access it.

        Comment

        • EARNEST
          New Member
          • Feb 2010
          • 128

          #5
          Aha, what I meant is either use that boolean result as a class variable (static) or store it as a string, and then check if stringName_Bool Result true or false, something like that.However, I'd go with your way, it is neater;

          Comment

          • tlhintoq
            Recognized Expert Specialist
            • Mar 2008
            • 3532

            #6
            By the way - you don't have to go through all that registry reading to determine if you are 32 or 64 bit. Just check the size of an IntPtr.

            If you are on a 32 bit O.S., then an IntPtr size is 4.
            If you are on a 64 bit O.S., then an IntPtr size is 8.

            Code:
            int bits = IntPtr.Size * 8;
            Console.WriteLine( "{0}-bit", bits );

            Comment

            • USBZ0r
              New Member
              • Mar 2009
              • 20

              #7
              thanks for the help guys, appreciate it!

              im going to try it now.

              Comment

              • USBZ0r
                New Member
                • Mar 2009
                • 20

                #8
                right, i have used your method to finding the platform, works a treat! thanks!

                i have a question reguarding the reuseable string though, if i were to do it in program.cs, does that mean it will execute the code when the app first runs, then remember what it was and reuse it when i need it again?

                i just want them to read registry once, rather than having to access the registry every time i need the values.

                Comment

                • tlhintoq
                  Recognized Expert Specialist
                  • Mar 2008
                  • 3532

                  #9
                  i have a question reguarding the reuseable string though, if i were to do it in program.cs, does that mean it will execute the code when the app first runs, then remember what it was and reuse it when i need it again?
                  Yes. The code in Program.cs runs before anything else runs. That is the code that causes your Form1 to be created.

                  i just want them to read registry once, rather than having to access the registry every time i need the values.
                  Read *what* registry? I already showed you that you don't need to read *any* registry to get the platform.
                  Code:
                  int bits = IntPtr.Size * 8;

                  Comment

                  • USBZ0r
                    New Member
                    • Mar 2009
                    • 20

                    #10
                    thanks for the reply.

                    i know it runs before everything else, what i mean was, can it remember what something was from the first time it was run? kinda hard to explain what i mean.

                    i use registry for a number of things, my program has its own registry structure.

                    i access the registry settings for my app several times, i want it to remember what the values were from the first time it checked rather then having to do it each time it needs to know the values again (it still must re-check every time the app loads atleast once though)

                    thanks for the help

                    Comment

                    • tlhintoq
                      Recognized Expert Specialist
                      • Mar 2008
                      • 3532

                      #11
                      i know it runs before everything else, what i mean was, can it remember what something was from the first time it was run? kinda hard to explain what i mean
                      Yes. Variables remember their values until you set them to something else.

                      Do you mean will it retain that value between different instances of running the application? No. No variables do that. Considering it is a simple calculation that takes like 1 CPU cycle, why would it matter?

                      i want it to remember what the values were from the first time it checked rather then having to do it each time it needs to know the values again
                      Then put the values in the Program.cs file like I showed you with the int Bits.

                      <taps on microphone> Hello... Is thing on?... Hello?

                      Comment

                      • USBZ0r
                        New Member
                        • Mar 2009
                        • 20

                        #12
                        right, thats what i needed to know, i just wasnt sure.

                        only reason im doing it is because m app is fairly registry heavy, i wanted to make it efficient as possibly by not having to keep accessing it for the same stuff, thats all.

                        Cheers

                        Comment

                        Working...