貳、疑問
c#獲取系統性能方法與疑問(1)中的方法是通過讀取wmi來獲取相關信息的,讀取cpu溫度發現不準,網上找了很多資料,很多人同樣的問題,視乎通過wmi是不可行的,
網絡類似問題描述1:
“使用 wmi 獲取CPU 溫度 後 總是一個靜態值, 重啓後值可能會改變,但是還是一直保持不變。”
希望高手們提點意見。
wmi方法不成功後,csdn上下了個類似的程序,是通過WinIo來實現的。
下載地址爲:C#通過調用WinIo獲取CPU溫度
問題又出現了
private void button7_Click(object sender, EventArgs e)
{
try
{
int temp = 0;
if (!WinIo.InitializeWinIo())
{
MessageBox.Show("WinIo初始化失敗!");
return;
}
Wait4Set();
bool tmp = WinIo.SetPortVal(0x6C, 0x80, 1);//將命令寫入命令端口0x6C
Wait4Set();
tmp = WinIo.SetPortVal(0x68, 0xE7, 1);//將要訪問的寄存器號寫入數據端口0x68
Wait4Get();
tmp = WinIo.GetPortVal(0x68, ref temp, 1); ;//將數據從數據端口讀出
WinIo.ShutdownWinIo();
textBox1.Text = temp.ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Wait4Set()
{
int tmp = 0;
WinIo.GetPortVal(0x6c, ref tmp, 1);
if (tmp != 2 )
Wait4Set();
}
private void Wait4Get()
{
int tmp = 0;
WinIo.GetPortVal(0x6c, ref tmp, 1);
if (tmp != 1)
Wait4Set();
try
{
int temp = 0;
if (!WinIo.InitializeWinIo())
{
MessageBox.Show("WinIo初始化失敗!");
return;
}
Wait4Set();
bool tmp = WinIo.SetPortVal(0x6C, 0x80, 1);//將命令寫入命令端口0x6C
Wait4Set();
tmp = WinIo.SetPortVal(0x68, 0xE7, 1);//將要訪問的寄存器號寫入數據端口0x68
Wait4Get();
tmp = WinIo.GetPortVal(0x68, ref temp, 1); ;//將數據從數據端口讀出
WinIo.ShutdownWinIo();
textBox1.Text = temp.ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Wait4Set()
{
int tmp = 0;
WinIo.GetPortVal(0x6c, ref tmp, 1);
if (tmp != 2 )
Wait4Set();
}
private void Wait4Get()
{
int tmp = 0;
WinIo.GetPortVal(0x6c, ref tmp, 1);
if (tmp != 1)
Wait4Set();
}
會出現死循環,原因在於
Wait4Get()方法裏WinIo.GetPortVal(0x6c, ref tmp, 1);返回的tmp永遠不等於1.
網絡上查了下原因,視乎沒有解決方法,好像是c#不能訪問底層什麼的。
網絡回答1:
“你沒有實現2個wait的功能,即使實現了wait, 可能還是讀不到數據。
因爲win32 用戶程序,不可能直接讀取端口吧! 大部分端口都是被系統保護的。”
因爲win32 用戶程序,不可能直接讀取端口吧! 大部分端口都是被系統保護的。”
爲了測試我把Wait4Get去掉
int temp = 0;
if (!WinIo.InitializeWinIo())
{
MessageBox.Show("WinIo初始化失敗!");
return;
}
bool tmp = WinIo.SetPortVal(0x6C, 0x80, 1);//將命令寫入命令端口0x6C
tmp = WinIo.SetPortVal(0x68, 0xE7, 1);//將要訪問的寄存器號寫入數據端口0x68
tmp = WinIo.GetPortVal(0x68, ref temp, 1); //將數據從數據端口讀出
WinIo.ShutdownWinIo();
textBox1.Text = temp.ToString();
{
MessageBox.Show("WinIo初始化失敗!");
return;
}
bool tmp = WinIo.SetPortVal(0x6C, 0x80, 1);//將命令寫入命令端口0x6C
tmp = WinIo.SetPortVal(0x68, 0xE7, 1);//將要訪問的寄存器號寫入數據端口0x68
tmp = WinIo.GetPortVal(0x68, ref temp, 1); //將數據從數據端口讀出
WinIo.ShutdownWinIo();
textBox1.Text = temp.ToString();
結果WinIo.GetPortVal(0x68, ref temp, 1); 的返回值都爲-1;
根據網絡上的意思,c++或delphi通過嵌入彙編可以實現,後下載了個delphi程序,發現確實可以用,但是看不太懂,最麻煩的c#還是不能解決問題。
希望朋友們能提供點意見。