1 Replies - 464 Views - Last Post: 20 November 2011 - 12:31 AM Rate Topic: -----

#1 adn258   User is offline

  • D.I.C Addict

Reputation: 12
  • View blog
  • Posts: 816
  • Joined: 31-August 11

A String Or Variable Is Declared Within a Button Does it Use Memory?

Posted 20 November 2011 - 12:20 AM

Okay this question might sound dumb but it's not something I am sure about.

So for instance if you take the code below I use the string
string KeyShutValue = @"System\CurrentControlSet\Control"; Does that string remain in memory after the form is done loading?

  private void Form1_Load(object sender, EventArgs e)
            //for results and settings this uses
            string KeyShutValue = @"System\CurrentControlSet\Control";
            using (RegistryKey key = Registry.LocalMachine.OpenSubKey(KeyShutValue, true))
                //from registry convert to string the integer of the value divided by 1000
                lbl_WaitTime.Text = Convert.ToString(Convert.ToInt32(key.GetValue("WaitToKillServiceTimeout")) / 1000);

                //settings in registry and let the user know why we are doing this dividing by 1000 since we used times 1000
                trackBar1.Value = Convert.ToInt32(key.GetValue("WaitToKillServiceTimeout")) / 1000;
                trackBar1.Enabled = false;

                MessageBox.Show("If Certain Processes Are Running Like Task Manager, It Can Cause A Hangup When Trying To Shutdown The System.\n\n"
                                + "Someone Can Then Use This To Cancel The Shutdown:  THIS IS WHY WE HIGHLY ADVISE SETTING THIS TO ZERO USING THE BUTTON\n\n"
                                + "ABOVE.  If You Want To Set Your Own 'Custom' Value To This You May Do So By Using The\n\n"
                                + "'Set Custom Value Button', And Then Moving The Slide Bar To However Many Seconds You Want", "We Advise This To Be Set To Zero, What Do You Want?", MessageBoxButtons.OK, MessageBoxIcon.Warning);

Not in a button click say we took the code below the string
string KeyName = @"System\CurrentControlSet\Control";
Would that string take up memory BEFORE the button is clicked? Would it take up memory after the button is clicked OR BOTH? I'm a little confused on that

private void btnAccept_ShutWaitTime_Click(object sender, EventArgs e)
            string KeyName = @"System\CurrentControlSet\Control";
            using (RegistryKey key = Registry.LocalMachine.OpenSubKey(KeyName, true)) 
                key.SetValue("WaitToKillServiceTimeout", trackBar1.Value * 1000, RegistryValueKind.String);

                if (trackBar1.Value != 0)
                    MessageBox.Show("The Computer's Wait Shutdown Time Is Set To The Value Specified"
                                    + " Please Note: Their Are Security Risks Since This Isn't Set To Zero. You Can Set This Again Later In The Program Itself", "Value Set", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    MessageBox.Show("Value Set Safely To Zero As Advised.  You Can Set This Again Later In The Program.!!", "Set To Zero", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);



Is This A Good Question/Topic? 0
  • +

Replies To: A String Or Variable Is Declared Within a Button Does it Use Memory?

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5465
  • View blog
  • Posts: 14,418
  • Joined: 18-April 07

Re: A String Or Variable Is Declared Within a Button Does it Use Memory?

Posted 20 November 2011 - 12:31 AM

The event is created by the operating system, captured by the program, calls the click event and as that event is being executed, when it reaches the "string KeyName =" is the exact moment that memory is fetched and given to the variable.

This variable then goes out of scope when the event ends and marked for garbage collection which may be run right then or later.

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1