Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8376015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:19:24+00:00 2026-06-09T15:19:24+00:00

I guess the problem is the too much foreach loops there are. But i

  • 0

I guess the problem is the too much foreach loops there are.
But i need them to get the sensor.Value

This is the two functions:

private void cpuView()
        {

                Computer myComputer = new Computer();
                myComputer = new Computer(settings) { CPUEnabled = true };

                myComputer.Open();
                Trace.WriteLine("");
                foreach (var hardwareItem in myComputer.Hardware)
                {
                    if (hardwareItem.HardwareType == HardwareType.CPU)
                    {
                        hardwareItem.Update();
                        foreach (IHardware subHardware in hardwareItem.SubHardware)
                            subHardware.Update();

                        foreach (var sensor in hardwareItem.Sensors)
                        {
                            settings.SetValue("sensor", sensor.Value.ToString());
                            if (sensor.SensorType == SensorType.Temperature)
                            {
                                sensor.Hardware.Update();
                                settings.GetValue("sensor", sensor.Value.ToString());
                                temperature_label.Text = sensor.Value.ToString() + "c";//String.Format("{0} Temperature = {1}c", sensor.Name, sensor.Value.HasValue ? sensor.Value.Value.ToString() : "no value");
                            }
                        }
                    }
                }
        }

And the second function:

private void gpuView()
        {


                Computer computer = new Computer();
                computer.Open();
                computer.GPUEnabled = true;

                foreach (var hardwareItem in computer.Hardware)
                {
                    if (videoCardType("ati", "nvidia") == true)
                    {
                        HardwareType htype = HardwareType.GpuNvidia;

                        if (hardwareItem.HardwareType == htype)
                        {

                            foreach (var sensor in hardwareItem.Sensors)
                            {

                                if (sensor.SensorType == SensorType.Temperature)
                                {

                                    sensor.Hardware.Update();
                                    if (sensor.Value.ToString().Length > 0)
                                    {
                                        if (newGPULabel.Text.Length < 1)
                                        {
                                            if (UpdatingLabel(sensor.Value.ToString(), string.Empty))
                                            {
                                                label8.Text = newGPULabel.Text;
                                            }
                                        }
                                        else if (UpdatingLabel(sensor.Value.ToString(), newGPULabel.Text.Substring(0, newGPULabel.Text.Length - 1)))
                                        {
                                            label8.Text = newGPULabel.Text;
                                        }
                                        newGPULabel.Text = sensor.Value.ToString() + "c";
                                        label8.Visible = true;
                                    }

                                    int t = newGPULabel.Text.Length;
                                    if (t >= 4)
                                    {
                                        newGPULabel.Location = new Point(210, 100);

                                    }
                                    else
                                    {
                                        newGPULabel.Location = new Point(250, 100);
                                    }
                                    timer2.Interval = 1000;
                                    if (sensor.Value > 90)
                                    {
                                        Logger.Write("The current temperature is ===> " + sensor.Value);
                                        button1.Enabled = true;
                                    }
                                    this.Select();
                                }
                            }
                        }
                    }
                    else
                    {
                        HardwareType htype = HardwareType.GpuAti;

                        if (hardwareItem.HardwareType == htype)
                        {

                            foreach (var sensor in hardwareItem.Sensors)
                            {

                                if (sensor.SensorType == SensorType.Temperature)
                                {

                                    sensor.Hardware.Update();
                                    if (sensor.Value.ToString().Length > 0)
                                    {
                                        if (newGPULabel.Text.Length < 1)
                                        {
                                            if (UpdatingLabel(sensor.Value.ToString(), string.Empty))
                                            {
                                                label8.Text = newGPULabel.Text;
                                            }
                                        }
                                        else if (UpdatingLabel(sensor.Value.ToString(), newGPULabel.Text.Substring(0, newGPULabel.Text.Length - 1)))
                                        {
                                            label8.Text = newGPULabel.Text;
                                        }
                                        newGPULabel.Text = sensor.Value.ToString() + "c";
                                        label8.Visible = true;
                                    }

                                    int t = newGPULabel.Text.Length;
                                    if (t >= 4)
                                    {
                                        newGPULabel.Location = new Point(210, 100);

                                    }
                                    else
                                    {
                                        newGPULabel.Location = new Point(250, 100);
                                    }
                                    timer2.Interval = 1000;
                                    if (sensor.Value > 90)
                                    {
                                        Logger.Write("The current temperature is ===> " + sensor.Value);
                                        button1.Enabled = true;
                                    }
                                    this.Select();
                                }
                            }
                        }
                    } 
            }
        }

And in the timer2 tick event:

private void timer2_Tick(object sender, EventArgs e)
        {

            gpuView();
            cpuView();


        }

If i dont call this functions in the tick event the program is running smooth but even if im calling only one of the functions its stuck each n seconds.

The timer2 is set to interval 100 since im updating the cpu and gpu temperature i want to update them fast.
Im not sure if the problem is the too much foreach loops in the functions or that the interval is 100 so its trying to get the values from the sensors too fast so the hardware cant sned the information too fast.

Maybe there is a way to remove some of the foreach loops ?
I dont want to upload all my Form1 code to here . But all the snesors and hardware are connecting with the OpenHardwareMonitor.dll of the OpenHardwareMonitorApplication.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T15:19:25+00:00Added an answer on June 9, 2026 at 3:19 pm

    You can use a backgroundworker,it allow you doing multithreading on an easy way.
    it will be useful on your case and call doWork method on asynchronous way.In this method call your two functions on a loop.
    Here msdn explanation http://msdn.microsoft.com/en-us/library/cc221403(v=vs.95).aspx
    it is better than a timer that you set an random interval value because you have to let your functions complete so if an event have to occur it won’t ….

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably an easy one, but I'm missing something I guess. The problem
I know this looks embarrassingly easy, and I guess the problem is that I
My Problem is the following. I got this code and i guess a corrupt
I've searched for two days trying to find a solution to this. My problem
This question goes beoynd just programming, but I'd like to get some input on
I am not too much onto data mining but I require some ideas on
I guess the problem is that I do not know how to use factory
i am getting a problem while populating data inside the dropdown... I guess since
I guess getView() is called for each item of the list, but my question
I guess my general question which I googled to no luck: Is there a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.