Quantcast
Channel: Windows Forms General forum
Viewing all articles
Browse latest Browse all 12583

How do i use the backgroundworker can't figure out why it's not working ? Edited

$
0
0

I have this class with this functions inside:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenHardwareMonitor.Hardware;
using System.Diagnostics;
using System.Management;
using System.Windows.Forms;
using DannyGeneral;

namespace HardwareMonitoring
{
    class Hardware
    {
        public static Form1 form1;
        public static CpuTemperature settings;
        public static ManagementObjectSearcher searcher;

        public Hardware(Form1 f1)
        {
            form1 = f1;
             
        }

        public static void cpuView(bool pauseContinue, List<string> myData, ListBox lb1, Button bt1, float? tempcpuv)
        {
            if (pauseContinue == true)
            {
            }
            else
            {
                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());
                                //label17.Text = sensor.Value.ToString() + "c";//String.Format("{0} Temperature = {1}c", sensor.Name, sensor.Value.HasValue ? sensor.Value.Value.ToString() : "no value");

                                myData.Add("Gpu Temeprature --- " + sensor.Value.ToString());
                                lb1.DataSource = null;

                                tempcpuv = sensor.Value;
                                if (sensor.Value > 60)
                                {
                                    Logger.Write("The Current CPU Temperature Is ===> " + sensor.Value);
                                    bt1.Enabled = true;
                                }

                                break;
                            }
                        }
                    }
                }
            }
        }

        public static void gpuView(bool pauseContinue, List<string> myData, ListBox lb1, Button bt1, float? tempgpuv, Label lb4)
        {

            if (pauseContinue == true)
            {
            }
            else
            {
                Computer computer = new Computer();
                computer.Open();
                computer.GPUEnabled = true;

                foreach (var hardwareItem in computer.Hardware)
                {
                    if (videoCardType("ati", "nvidia",lb4) == 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)
                                    {

                                        /* else if (UpdatingLabel(sensor.Value.ToString(), label16.Text.Substring(0, label16.Text.Length - 1)))
                                         {
                                             //  Label8 = GpuText;
                                         }*/
                                        // data = new List<string>();
                                        myData.Add("Cpu Temeprature --- " + sensor.Value.ToString());
                                        lb1.DataSource = null;
                                        //sensor.Value.ToString() + "c";
                                        tempgpuv = sensor.Value;
                                        //label8.Visible = true;
                                    }
                                    if (sensor.Value > 60)
                                    {
                                        Logger.Write("The Current Nvidia GPU Temperature Is ===> " + sensor.Value);
                                        bt1.Enabled = true;
                                    }
                                    //form1.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)
                                    {

                                        tempgpuv = sensor.Value;
                                        //label8.Visible = true;
                                    }
                                    if (sensor.Value > 60)
                                    {
                                        Logger.Write("The Current Ati GPU Temperature Is ===> " + sensor.Value); // to check when writing to the log each second its writing the snesor value a lot of time in gpu and cpu
                                        // to check why to make that it will write every second or every n times once. and not a lot of times maybe to change the logger write place not in loop ?
                                        bt1.Enabled = true;
                                    }
                                    form1.Select();
                                }
                            }
                        }
                    }
                }
            }
        }

         public static bool videoCardType(string ati , string nvidia, Label lb4)
        {
            bool atiNvidia = false;
            string graphicsCard = string.Empty;
            foreach (ManagementObject mo in searcher.Get())
            {
                foreach (PropertyData property in mo.Properties)
                {
                    if (property.Name == "Description")
                    {
                        graphicsCard = property.Value.ToString();
                    }
                }
            }

            if (graphicsCard.Contains("NVIDIA") || graphicsCard.Contains("nvidia"))
            {
                atiNvidia = true;
                string toPrintNvidia = nvidia;
                if (graphicsCard.ToUpper().Contains(toPrintNvidia.ToUpper()))
                {
                    int startingIndex = graphicsCard.ToUpper().IndexOf(toPrintNvidia.ToUpper());
                    graphicsCard = graphicsCard.Substring(startingIndex, toPrintNvidia.Length);
                }
                lb4.Text = graphicsCard;
                return atiNvidia;
            }
            if (graphicsCard.Contains("ATI") || graphicsCard.Contains("ati"))
            {
                atiNvidia = false;
                string toPrintAti = ati;
                if (graphicsCard.ToUpper().Contains(toPrintAti.ToUpper()))
                {
                    int startingIndex = graphicsCard.ToUpper().IndexOf(toPrintAti.ToUpper());
                    graphicsCard = graphicsCard.Substring(startingIndex, toPrintAti.Length);
                }

                lb4.Text = graphicsCard;
                return atiNvidia;
            }

            return atiNvidia;
        }
    }
}

Then in Form1 constructor im doing:

using OpenHardwareMonitor.Hardware;
using System.Management;


 h = new Hardware(this);
            Hardware.settings = new CpuTemperature(new Dictionary<string, string>
            {
                { "/intelcpu/0/temperature/0/values", "H4sIAAAAAAAEAOy9B2AcSZYlJi9tynt/SvVK1+B0oQiAYBMk2JBAEOzBiM3mkuwdaUcjKasqgcplVmVdZhZAzO2dvPfee++999577733ujudTif33/8/XGZkAWz2zkrayZ4hgKrIHz9+fB8/Iu6//MH37x79i9/+NX6N3/TJm9/5f/01fw1+fosnv+A/+OlfS37/jZ/s/Lpv9fff6Ml/NTef/yZPnozc5679b+i193//TQZ+/w2Dd+P9/sZeX/67v/GTf/b3iP3u4/ObBL//73+i+f039+D8Zk/+xz/e/P6beu2TQZju8yH8f6OgzcvPv/U3/Rb8+z/0f/9b/+yfaOn8079X6fr6Cws7ln/iHzNwflPv99/wyS/+xY4+v/evcJ+733+jJ5//Cw7/4ndy9Im3+U2e/Fbnrk31C93vrt/fyPvdb+N//hsF7/4/AQAA//9NLZZ8WAIAAA==" },
                { "/intelcpu/0/load/0/values", "H4sIAAAAAAAEAOy9B2AcSZYlJi9tynt/SvVK1+B0oQiAYBMk2JBAEOzBiM3mkuwdaUcjKasqgcplVmVdZhZAzO2dvPfee++999577733ujudTif33/8/XGZkAWz2zkrayZ4hgKrIHz9+fB8/Iu6//MH37x79i9++mpwcv/md/9df89egZ/xX/ym/5y/4D37618Lv7ya//u+58+u+5d9/z7/5t/w9/6u5fP5bH/6av+eTkXyefXxp26ONaf/v/dG/sf39D/rvnv4e5vc/0IP56/waK/vuHzf5I38P8/tv+mv8Rbb9f0pwTF9/zr/1X9vP/8I//+/6Pf7Z30N+/zdf/HX29zd/859q4aCNP5b//U+U3/+7f+zXOjZwfqvDX/V7/o9/vPz+a1G/pv0f+fGlhfk7eZ//N3/0v28//5X0u/n8Cxq7+f1X/tHft20A5x8a/W5/02+BP36Nf+j/nv8XfzrT+c2//Ob4p3+vktvUhNs/+xcWikP6e/4T/5jS5M8/sL8vP/5ff49f/Ivl9//sHzv6PX/vXyG//9R/94/9HuZ34P/5vyC//3W/5e/1exa/k+Bw4bUBnU2bP4Xg/1bn0uafeTH6PatfKL//N3/0t2y/gG9+/8+IzqYNxmU+/+jwX7afY67/nwAAAP//GYSA31gCAAA=" },
            });
            Hardware.searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DisplayConfiguration");
            Hardware.videoCardType("ati", "nvidia",label4);
            data = new List<string>();

Then i have backgroundworker event dowork:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            while (true)
            {
                if ((worker.CancellationPending == true))
                {
                    e.Cancel = true;
                    break;
                }
                else
                {
                    if (tempCpuValue >= (float?)nud1.Value || tempGpuValue >= (float?)nud1.Value)
                    {
                        soundPlay = true;
                        blinking_label();
                        NudgeMe();
                    }
                    else
                    {
                        soundPlay = false;
                        stop_alarm = true;

                    }
                }
                data = new List<string>();
                Hardware.cpuView(pauseContinueDoWork, data, listBox1, button1, tempCpuValue);
                Hardware.gpuView(pauseContinueDoWork, data, listBox1, button1, tempGpuValue, label4);
                listBox1.DataSource = data;
                listBox1.Invalidate();
            }
        }

I know the DoWork event not should look like this and i don't know why now i don't see nothing in the listBox.

I have this listBox1 two events in Form1:

private void listBox1_MeasureItem(object sender, MeasureItemEventArgs e)
        {
            e.ItemHeight = 21;
        }

        private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index == -1)
            {
            }
            else
            {

                ColorText.ColorListBox(data, e);

            }
        }

I used a breakpoint on the ColoText.ColorListBox(data, e); and it never get there. So i guess that something is wrong with the DoWork event since when updating the listBox should activate this events.

What I need is to update every second this two methos gpuView() and cpuView() and also this part in the dowork:

if (tempCpuValue >= (float?)nud1.Value || tempGpuValue >= (float?)nud1.Value)
                    {
                        soundPlay = true;
                        blinking_label();
                        NudgeMe();
                    }
                    else
                    {
                        soundPlay = false;
                        stop_alarm = true;

                    }

But im not sure how to do it at all. I keep trying but still not get it.



Viewing all articles
Browse latest Browse all 12583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>