Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 131,603 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 2,042 people online right now. Registration is fast and FREE... Join Now!




RAM Information

 
Reply to this topicStart new topic

RAM Information

gbertoli3
post 8 Oct, 2008 - 03:34 PM
Post #1


DIC at Heart + Code

Group Icon
Joined: 23 Jun, 2008
Posts: 1,024



Thanked 16 times

Dream Kudos: 950
My Contributions


I am trying to get the percent of RAM used / unused, then put that value into a ProgressBar. For some reason I can't get it to work.

Here is what I am doing.

csharp

System.Management.SelectQuery query = new System.Management.SelectQuery("Win32_MemoryArray");
System.Management.ManagementObjectSearcher searcher = new System.Management.ManagementObjectSearcher(query);

Int32 max = 0;
Int32 final = 0;
foreach (System.Management.ManagementObject ManageObject in searcher.Get())
{
max = Int32.Parse(ManageObject["EndingAddress"].ToString());
Decimal gigs = Decimal.Divide(new System.Diagnostics.PerformanceCounter("Memory", "Available KBytes", "_Total").RawValue, 1000000);
final = max - (int)gigs;
ramProgressBar.Maximum = max / 100000;
}

ramProgressBar.Value = (int)new System.Diagnostics.PerformanceCounter("Memory", "% Committed Bytes In Use").RawValue / 100000;
User is offlineProfile CardPM

Go to the top of the page


gabehabe
post 8 Oct, 2008 - 04:10 PM
Post #2


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,371



Thanked 94 times

Dream Kudos: 2625

Expert In: Dingleberries

My Contributions


So what's not working? Is the progress bar not displaying correctly? Does it not compile?

Help us to help you smile.gif


EDIT:
From a quick read, you're not actually Increment()ing your progress bar anywhere, so it's always going to be empty.

You just simply change the maximum value of the progress bar a bunch of times.
User is offlineProfile CardPM

Go to the top of the page

Jayman
post 8 Oct, 2008 - 04:16 PM
Post #3


Student of Life

Group Icon
Joined: 26 Dec, 2005
Posts: 6,819



Thanked 38 times

Dream Kudos: 500

Expert In: C#, VB.NET, Java

My Contributions


A couple of problems here. The first is there is no need for an instance name for the performance counter, since you only have one. Secondly, there is a problem with how you are determining the percentage.

See the corrected code with comments:
csharp

System.Management.SelectQuery query = new System.Management.SelectQuery("Win32_MemoryArray");
System.Management.ManagementObjectSearcher searcher = new System.Management.ManagementObjectSearcher(query);
System.Diagnostics.PerformanceCounter performanceCounter = new System.Diagnostics.PerformanceCounter("Memory", "Available KBytes", true);

//To avoid unnecessary casting, make these all a double
double max = 0;
double final = 0;
double percentUsed = 0;
foreach (System.Management.ManagementObject ManageObject in searcher.Get())
{
max = Int32.Parse(ManageObject["EndingAddress"].ToString());
Decimal gigs = Decimal.Divide(performanceCounter.RawValue, 1000000);

//divide the RawValue by the max to get the percentage used
//then multiply by 100 to convert it to a value between 0 and 100
percentUsed = (performanceCounter.RawValue / max) * 100.0;
final = max - Convert.ToDouble(gigs);
ramProgressBar.Maximum = 100;
}

ramProgressBar.Value = (int)percentUsed;
User is offlineProfile CardPM

Go to the top of the page

gbertoli3
post 8 Oct, 2008 - 04:48 PM
Post #4


DIC at Heart + Code

Group Icon
Joined: 23 Jun, 2008
Posts: 1,024



Thanked 16 times

Dream Kudos: 950
My Contributions


Thanks jayman it worked
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/20/08 04:12AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month