2015-07-21 79 views
0

我真的寫在C#程序跟蹤CPU使用率,但我不能得到一個錯誤解決,無法隱式轉換烏龍爲int

「無法隱式轉換烏龍爲int」。下面

代碼:

 int i; 
     i = 0; 
     try 
     { 
      ManagementClass cpuDataClass = new ManagementClass("Win32_PerfFormattedData_PerfOS_Processor"); 

      while (true) 
      { 
       ManagementObjectCollection cpuDataClassCollection = cpuDataClass.GetInstances(); 
       foreach (ManagementObject obj in cpuDataClassCollection) 
       { 
        if (obj["Name"].ToString() == "_Total") 
        { 
         i = Convert.ToUInt64(obj["C1TransitionsPersec"]); 
        } 
       } 
       Thread.Sleep(100); 
      } 
     } 
     catch (ThreadAbortException tbe) 
     { 

     } 


     progressBar1.Maximum = 100; 
     progressBar1.Minimum = 1; 

     progressBar1.Value = i; 
     } 

我真的相當新的C#,所以我希望這是一個簡單的辦法,任何人都可以幫助我在這?

+3

而不是int i;做我的; – Yohannes

+0

修復了上述主要錯誤,但現在progressBar1.value發生了該錯誤 –

+0

progressBar1.Value =(int)i; //閱讀更多關於在這裏投射https://msdn.microsoft.com/en-us/library/ms173105.aspx – Yohannes

回答

0

錯誤信息本身就已說明..

iintConvert.ToUInt64回報ulong並沒有從之間要ulong因爲int的int類型沒有足夠的範圍,以保持一個ulong值沒有隱式的談話。

作爲一種快速解決方案,將您的數據類型更改爲ulong as;

ulong i; 

,但你仍然得到

progressBar1.Value = i; 

線路有問題,因爲Value propertyint類型。即使您將其轉換爲(int)i,如果您的i大於int.MaxValue或小於int.MinValuechecked模式,您將得到不同的值。

0

YTAM的揚聲器工作原理:

ulong i; ProgessBar1.value =(int)i;

相關問題