c# GPU HW读取、比较、返回

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;//启动时钟
using System.IO;//文本读写
using System.Threading;//多线层启动
using System.Diagnostics;


namespace GPUHW
{
    class Program
    {
        public static string[] ConfigVaule = { "NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL","NULL" };
        public static double[] GPUNumberValues = { 0,0};
        public static Thread CallBathThread;
        public static Thread CallTimerThread;
        public static bool start = false;
        static int Main(string[] args)
        {
            if(ReadConfig("config.ini")==false) return 1;


            //启动多线层调用批处理
            CallBathThread = new Thread(new ThreadStart(CallBath));
            CallBathThread.IsBackground = true;
            CallBathThread.Start();


            //启动多线层调用时钟
            CallTimerThread = new Thread(new ThreadStart(CallTimer));
            CallTimerThread.IsBackground = true;
            CallTimerThread.Start();


            while(start==false)
            {
            }
            if (CompareDataShow() == false) return 1;


            CallBathThread.Abort();//结束线层1
            CallBathThread.Join();//结束线层1


            //关闭进程2
            CallTimerThread.Abort();
            CallTimerThread.Join();
            return 0;
        }


        public static bool CompareDataShow()
        {
            bool Flag = false;
            double GPU_Temperature_L = 0, GPU_Temperature_H = 0;
            double Fan_Speed_L = 0, Fan_Speed_H = 0;
            GPU_Temperature_L = Convert.ToDouble(ConfigVaule[4]);
            GPU_Temperature_H = Convert.ToDouble(ConfigVaule[5]);
            Fan_Speed_L = Convert.ToDouble(ConfigVaule[6]);
            Fan_Speed_H = Convert.ToDouble(ConfigVaule[7]);
            Console.WriteLine("WINDOWS GPU  HW Test Tool V1.00 -Ferex Incorporation,By Shenbo,2018-05-11");
            Console.WriteLine("========================================================================================================");
            Console.WriteLine("Item_Name\t\tUpper\t\tLower\t\tTest_Value\t\tResult");
            if (((GPUNumberValues[0] > GPU_Temperature_L) && (GPUNumberValues[0] < GPU_Temperature_H))&&
                ((GPUNumberValues[1] > Fan_Speed_L) && (GPUNumberValues[1] < Fan_Speed_H)))
                Flag = true;
            else
                Flag = false;
            string GPUStr = string.Format("{0:F}", GPUNumberValues[0]);//保留一位字符串
            string FANStr = string.Format("{0:F}", GPUNumberValues[1]);//保留一位字符串


            ShowResult(Flag, "GPU_Temperature\t\t  " + ConfigVaule[4].Trim() +"C"+ "\t\t" + ConfigVaule[5].Trim() +"C"+"\t\t", GPUStr +"C"+ "\t\t\t");
            ShowResult(Flag, "Fan_Speed\t\t" + ConfigVaule[6].Trim() +"PRM"+ "\t\t" + ConfigVaule[7].Trim() +"PRM"+ "\t\t", FANStr+"PRM" + "\t\t");
            return Flag;
        }


        public static void ShowResult(bool IsNo,string str1,string str2)
        {
            Console.ForegroundColor = ConsoleColor.Gray;
            Console.Write(str1);
            if(IsNo==false)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Write(str2 + "Fail\n");
                Console.ResetColor();
            }
            else
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.Write(str2 + "Pass\n");
                Console.ResetColor();
            }
        }


        public static bool ReadKeyValues(string ReadKeyValuesFileName)//读取配置中的数值
        {
            bool Flag = false;
            int n = 0;
            try
            {
                FileStream fs = new FileStream(ReadKeyValuesFileName,FileMode.Open,FileAccess.Read);
                StreamReader sr = new StreamReader(fs,Encoding.Default);
                string Temp = string.Empty;
                while((Temp=sr.ReadLine())!=null)
                {
                    string[] Array = Temp.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                    if(n>=3)
                    {
                        GPUNumberValues[0] = (Convert.ToDouble(Array[3].Trim())) + GPUNumberValues[0];
                        GPUNumberValues[1] = (Convert.ToDouble(Array[5].Trim())) + GPUNumberValues[1];
                    }


                    n++;
                }
                GPUNumberValues[0] = GPUNumberValues[0] / (n - 2);
                GPUNumberValues[1] = GPUNumberValues[1] / (n - 2);
                sr.Close();
                fs.Close();
                Flag = true;
            }
            catch(Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Read Key Values Err:"+ex.Message);
                Console.ResetColor();
                Flag = false;
            }
            return Flag;
        }


        public static void CallBath()//调用批处理
        {
            string targetDir = string.Empty;
            //bool Falg = false;
            targetDir = System.IO.Directory.GetCurrentDirectory() + @"\";
            Process proc = null;
            try
            {
                proc = new Process();
                proc.StartInfo.WorkingDirectory = targetDir;
                proc.StartInfo.FileName = ConfigVaule[0];
                proc.Start();
                proc.WaitForExit();
                //Falg = true;
            }
            catch(Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Call Bath Run Err:"+ex.Message);
                Console.ResetColor();
                System.Environment.Exit(1);
                //Falg = false;
            }
            //return Falg;
        }


        public static bool ReadConfig(string ConfigName)//读取配置文件
        {
            bool Flag = false;
            string[] SeekStr = { "RunBath", "ReadLog", "RunBathTimer", "KillProgramName", "GPU_Temperature_L", "GPU_Temperature_H", "Fan_Speed_L", "Fan_Speed_H", "CreateFileName" };
            try
            {
                FileStream fs = new FileStream(ConfigName,FileMode.Open,FileAccess.Read);
                StreamReader sr = new StreamReader(fs,Encoding.Default);
                string Temp = string.Empty;
                while((Temp=sr.ReadLine())!=null)
                {
                    string[] Array = Temp.Split(new string[]{"="},StringSplitOptions.RemoveEmptyEntries);
                    int n = 0;
                    foreach(string str in SeekStr)
                    {
                        if(str.ToUpper()==Array[0].Trim().ToUpper())
                        {
                            ConfigVaule[n] = Array[1].Trim();
                            break;
                        }
                        n++;
                    }
                }
                sr.Close();
                fs.Close();
                Flag = true ;
            }
            catch(Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Config File "+ConfigName+" Read Err:"+ex.Message);
                Console.ResetColor();
                Flag = false;
            }
            return Flag;
        }


        private static void CallTimer()//定义时启动时钟
        {
            System.Timers.Timer t = new System.Timers.Timer(Convert.ToInt32(ConfigVaule[2])*1000);
            //Timer t = new Timer(times);
            t.Enabled = true;
            t.Elapsed += T_Elapsed;
        }
        private static void T_Elapsed(object sender,ElapsedEventArgs e)//启动进钟
        {
            KillProcess();//结束进程
            Thread.Sleep(1500);
            ReadKeyValues(ConfigVaule[8]);//读取生成记录数据
            start = true;
        }


        public static void KillProcess()//查找进程,结束进程
        {
            Process[] pro = Process.GetProcesses(); //获取已开启的所有进程
            //遍历所有查找到的进程
            for(int i=0;i<pro.Length;i++)
            {
                if(pro[i].ProcessName.ToString()==ConfigVaule[3])
                {
                    pro[i].Kill();//结束进程
                }
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/u013934107/article/details/80288912
GPU