Thread.cpp
上传用户:haifei
上传日期:2022-07-20
资源大小:77k
文件大小:5k
源码类别:

网络编程

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include <winperf.h>
  3. #include <stdio.h>
  4. #include <pdh.h>
  5. #include <pdhmsg.h>
  6. #include "AnalogMeterTestView.h"
  7. #include "RegPerf.h"
  8. BOOL bStop=TRUE;
  9. //
  10. //  仪表1监视线程  
  11. //
  12. void  Meter1_thread(LPVOID pParam)
  13. {
  14.     CAnalogMeterTestView * pView = (CAnalogMeterTestView *)pParam;
  15. CString name = pView->m_strComputer1;
  16. //
  17. // set current thread to HIGHEST PRIORITY
  18. //
  19. ::SetThreadPriority(::GetCurrentThread(),THREAD_PRIORITY_HIGHEST);
  20.  
  21. //
  22.     // PDH Query Handle for these counters
  23. //
  24.     static HQUERY   hQuery = NULL;
  25.     HCOUNTER hCounter;
  26.     PDH_STATUS  pdhStatus;
  27.     PDH_RAW_COUNTER RawValue ;// counter value 
  28.     PDH_COUNTER_PATH_ELEMENTS  pdh_Path;
  29. name = "\\"+name;
  30.     pdh_Path.szMachineName = name.GetBuffer(name.GetLength());
  31.     pdh_Path.szObjectName = "Processor";
  32.     pdh_Path.szInstanceName = "0";
  33.     pdh_Path.szParentInstance = NULL;
  34.     pdh_Path.dwInstanceIndex = 0;
  35.     pdh_Path.szCounterName = "% Processor Time";
  36.     CHAR  szCounterBuffer[MAX_PATH];
  37. DWORD  length = MAX_PATH;
  38.     PdhMakeCounterPath ( &pdh_Path,
  39.                 szCounterBuffer,
  40. &length,
  41. 0);
  42.     DWORD     dwType;
  43.     PDH_FMT_COUNTERVALUE    pValue;
  44. int i,hello;
  45.     if (hQuery == NULL) 
  46. {
  47.         pdhStatus = PdhOpenQuery (NULL, 0, &hQuery);
  48.     }
  49.     //
  50.     // try to add the counter to the query
  51. //
  52.     pdhStatus = PdhAddCounter (hQuery,
  53.                        szCounterBuffer, 
  54.    0, 
  55.    &hCounter);
  56.     if (pdhStatus != ERROR_SUCCESS) 
  57. {
  58.     printf("PdhAddCounter  Error!n");
  59.         switch(pdhStatus)
  60. {
  61. case PDH_CSTATUS_BAD_COUNTERNAME:
  62. break;
  63. case PDH_CSTATUS_NO_COUNTER :
  64. break;
  65. case PDH_CSTATUS_NO_COUNTERNAME :
  66. break;
  67. case PDH_CSTATUS_NO_MACHINE  :
  68. break;
  69. default:
  70. hello=1;
  71. }
  72. return ;
  73. }
  74.     pdhStatus = PdhCollectQueryData (hQuery);
  75.     if (pdhStatus != ERROR_SUCCESS) 
  76. {
  77.         printf("PdhCollectQueryData Loop  Error! n");
  78.     return;
  79. }
  80.     Sleep(1000);
  81. // collect data 
  82. while(bStop)
  83. {
  84.         // get the current values of the query data
  85.         pdhStatus = PdhCollectQueryData (hQuery);
  86.         if (pdhStatus != ERROR_SUCCESS) 
  87. {
  88.             printf("PdhCollectQueryData Loop  Error! n");
  89.     return;
  90. }
  91.         pdhStatus = PdhGetFormattedCounterValue( hCounter, 
  92.                                          PDH_FMT_DOUBLE, 
  93.      &dwType, 
  94.      &pValue);
  95.         if(pValue.doubleValue<0) 
  96.  pValue.doubleValue = (FLOAT)0.0f;
  97. pView->m_lastAngleDeg[0] = pValue.doubleValue;
  98.         pView->m_bChanged[0] = TRUE;
  99. Sleep(1000);
  100. }    
  101. PdhCloseQuery(hQuery);
  102. }
  103. #define IsLocalComputer(a , LocalComputerName) (!lstrcmp(a,LocalComputerName))
  104. #define IsRemoteComputer(a) (!IsLocalComputer(a))
  105. #define szComputerPrefix         TEXT("\\") 
  106. //
  107. //  仪表2监视线程  
  108. //
  109. void  Meter2_thread(LPVOID pParam)
  110. {
  111.     CAnalogMeterTestView * pView = (CAnalogMeterTestView *)pParam;
  112. CString MachineName = pView->m_strComputer2;
  113. // MachineName = "\\"+MachineName;
  114. HKEY   hkResult; 
  115. HKEY   hKey;
  116. TCHAR  LocalComputerName[MAX_COMPUTERNAME_LENGTH + 3];
  117. DWORD  dwLength; 
  118.   ::SetThreadPriority(::GetCurrentThread(),THREAD_PRIORITY_HIGHEST);
  119.     dwLength = MAX_COMPUTERNAME_LENGTH ;
  120. ::GetComputerName(LocalComputerName,&dwLength);
  121. LPCTSTR p = MachineName.GetBuffer(MachineName.GetLength());
  122. if(IsLocalComputer(LocalComputerName,p))
  123. { // local computer
  124. hKey = HKEY_PERFORMANCE_DATA;
  125. }
  126. else
  127. { // remote computer
  128.         MachineName = "\\"+MachineName;
  129.         LPTSTR p = MachineName.GetBuffer(MachineName.GetLength());
  130. LONG lRet = ::RegConnectRegistry (
  131.                          p,
  132.                          HKEY_PERFORMANCE_DATA,
  133.                          &hKey);
  134.     if(lRet!=ERROR_SUCCESS)
  135. {
  136.     AfxMessageBox("Can not to registry...");
  137.     return;
  138. }
  139. }
  140.     //
  141.     // Get the index for the object.
  142. //
  143.     char  szIndex[256] = "";
  144.     XYGetIndexByName( "Processor", szIndex ,hKey);
  145.     COMPUTE_DATA   PreCounter;
  146.     COMPUTE_DATA   CurrentCounter;    
  147.     FLOAT PerfData;
  148. //
  149. // collect first data
  150. //
  151.     XYGetRawData(&PreCounter,szIndex,hKey);
  152.     Sleep(1000);
  153.     
  154.     // collect data
  155. while(bStop)
  156. {
  157.         XYGetRawData(&CurrentCounter,szIndex,hKey);
  158.         PerfData=XYGetPerfData(PreCounter,CurrentCounter);
  159.         PreCounter.llRawData = CurrentCounter.llRawData;
  160.         PreCounter.llData100NS =CurrentCounter.llData100NS;
  161. pView->m_lastAngleDeg[1] = PerfData;
  162.         pView->m_bChanged[1] = TRUE;
  163. Sleep(1000);
  164. }    
  165. // Close handle to the key.
  166.     RegCloseKey( hKey );
  167. }