6 Replies - 5202 Views - Last Post: 04 January 2012 - 05:30 PM

#1 elesbb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 06-December 11

Created a C# program, seems to eat memmory.

Posted 03 January 2012 - 05:34 PM

I wrote a simple C# program , and the memory footprint seems to get larger and larger over time. Can someone help tweak my code to keep this from happening ? thanks . Code is below. This is for WM6.5 btw


using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using System.Runtime.InteropServices;
using System.Threading;

namespace UnlockToHome
{
    class Program
    {
        [DllImport("coredll.dll")]
        private static extern IntPtr GetDesktopWindow();

        [DllImport("coredll.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);

        static void Main(string[] args)
        {
            int locked;

            while (true)
            {
                locked = (int)Registry.LocalMachine.OpenSubKey("System\\State").GetValue("Lock");
                while (locked == 0)
                {
                    Thread.Sleep(500);
                    locked = (int)Registry.LocalMachine.OpenSubKey("System\\State").GetValue("Lock");
                }
                while (locked > 0)
                {
                    Thread.Sleep(500);
                    locked = (int)Registry.LocalMachine.OpenSubKey("System\\State").GetValue("Lock");
                }
                if ((int)Registry.LocalMachine.OpenSubKey("SOFTWARE\\elesbb\\Unlock2Home").GetValue("Enabled") == 1)
                {
                    SetForegroundWindow(GetDesktopWindow());
                }
            }
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Created a C# program, seems to eat memmory.

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: Created a C# program, seems to eat memmory.

Posted 04 January 2012 - 06:51 AM

You are in an infinite loop.

That is some horrible code. What exactly are you trying to accomplish?
Was This Post Helpful? 0
  • +
  • -

#3 elesbb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 06-December 11

Re: Created a C# program, seems to eat memmory.

Posted 04 January 2012 - 10:55 AM

View Posteclipsed4utoo, on 04 January 2012 - 06:51 AM, said:

You are in an infinite loop.

That is some horrible code. What exactly are you trying to accomplish?


the code works. Like it does exactly what i want it to. Howevern , as i said before it slowly eats memory . And i needed the while(true) "infinite loop" in order to keep the program from terminating and to keep getting the registry values . The program simply displays the Home screen when you unlock it. I have a GUI which sets a registry entry for the "Enabled" value to simply toggle if you want the device to unlock to home or not . Is there a less brutal method for accomplishing this ? Maybe by tapping in to the device's locking API ?
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,596
  • Joined: 16-October 07

Re: Created a C# program, seems to eat memmory.

Posted 04 January 2012 - 11:46 AM

It's possible that a call to OpenSubKey creates more overhead than can be cleaned up in the allotted time frame. If the system is spinning so often that it doesn't have a chance to do cleanup, it will grow.

I'd call OpenSubKey only as often as required. I'd also consider waiting a little at the end of the loop.

I'd write your code like this:
using System;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;

namespace UnlockToHome {
	class Program {
		[DllImport("coredll.dll")]
		private static extern IntPtr GetDesktopWindow();

		[DllImport("coredll.dll")]
		private static extern bool SetForegroundWindow(IntPtr hWnd);

		private RegistryKey sysState, software;
		public Program() {
			RegistryKey lm = Registry.LocalMachine;
			sysState = lm.OpenSubKey("System\\State");
			software = lm.OpenSubKey("SOFTWARE\\elesbb\\Unlock2Home");
		}

		private bool isLocked() { return ((int)sysState.GetValue("Lock")) != 0; }
		private bool isEnabled() { return ((int)software.GetValue("Enabled")) == 1; }
		private void checkWait() { Thread.Sleep(500); }

		public void run() {
			while (!isLocked()) { checkWait(); }
			while (isLocked()) { checkWait(); }
			if (isEnabled()) { SetForegroundWindow(GetDesktopWindow()); }
			checkWait(); // wait again, give the system a chance to catch up
		}

		static void Main(string[] args) {
			Program pgm = new Program();
			while (true) { pgm.run(); }
		}
	}
}


Was This Post Helpful? 1
  • +
  • -

#5 elesbb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 06-December 11

Re: Created a C# program, seems to eat memmory.

Posted 04 January 2012 - 02:48 PM

that worked perfectly . thanks so much . now i have a favor , since im new to this , would you mind posting comments on every line of text explaining in detail what is happening ? there are some codes i havent used before , also the whole setup is new to me , ive only done things in the static void Main(string[] args) field and you only have 2 lines there xD

This post has been edited by JackOfAllTrades: 04 January 2012 - 03:54 PM
Reason for edit:: Removed unnecessary quote.

Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,596
  • Joined: 16-October 07

Re: Created a C# program, seems to eat memmory.

Posted 04 January 2012 - 04:11 PM

I simply reorganized your code. It's all your code.

The organization is basic object stuff. However, if it will help with understanding.
class Program {
	// ... externals defined ...
	
	// object level variables
	// to maintain references to other object for the life of this object
	private RegistryKey sysState, software;
	
	// constructor
	public Program() {
		// local variable, references the result of LocalMachine from Registry
		// note, by looking at documentation for LocalMachine and OpenSubKey
		// you will find that the result is of type RegistryKey
		RegistryKey lm = Registry.LocalMachine;
		// initialize the object variables
		this.sysState = lm.OpenSubKey("System\\State"); // one subkey often called
		this.software = lm.OpenSubKey("SOFTWARE\\elesbb\\Unlock2Home");  // another subkey often called
	}

	// return true or false, which is all we need
	private bool isLocked() { 
		// call the GetValue method of our sysState object
		// cast the result as int
		// if the value is 0, then it is not locked
		// otherwise it is
		return ((int)sysState.GetValue("Lock")) != 0;
	}
	
	// the same idea as above
	private bool isEnabled() { return ((int)software.GetValue("Enabled")) == 1; }

	// we want to wait for a time
	// by placing the wait here, we could change to value globally in one place
	private void checkWait() { Thread.Sleep(500); }

	public void run() {
		// wait until isLocked()==true
		while (!isLocked()) { checkWait(); }
		// this seems marginally redunant, but it was in your code
		while (isLocked()) { checkWait(); }
		
		// check the enabled state and if true preform action
		if (isEnabled()) { SetForegroundWindow(GetDesktopWindow()); }
		checkWait(); // wait again, give the system a chance to catch up
	}
}



The above is a class that defines an instance of an object.

static void Main(string[] args) {
	// create an instance of Program identified as pgm
	Program pgm = new Program();
	
	// an infinite loop, we run forever
	while (true) {
		// invoke the run method of our object
		pgm.run();
	}
}



To use C#, or most modern languages, effectively, you have to understand the fundamentals of object oriented programming (OOP). It's not particularly complex, just another way of grouping functions, but knowing the concept is necessary.
Was This Post Helpful? 0
  • +
  • -

#7 elesbb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 06-December 11

Re: Created a C# program, seems to eat memmory.

Posted 04 January 2012 - 05:30 PM

i appreciate that a lot . i honestly just jumped into C# only understanding commands and not really learning the fundamentals . i guess i should go read something somewhere . lol . i never did things like private bool or or did things outside of the static void Main(string[] args) { segment . ive done a lot of simple gui's for people . but never did any while statemenst that never ended . thanks for the help !

MOD Edit: If you are replying to the post directly above yours, please do not quote it.

This post has been edited by eclipsed4utoo: 05 January 2012 - 06:33 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1