ThreadPriorityDlg.cpp
上传用户:xsxdsb
上传日期:2009-12-14
资源大小:672k
文件大小:7k
源码类别:

书籍源码

开发平台:

Visual C++

  1. // ThreadPriorityDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "ThreadPriority.h"
  5. #include "ThreadPriorityDlg.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. static UINT g_uCount1 = 0;
  12. static UINT g_uCount2 = 0;
  13. static UINT g_uCount3 = 0;
  14. static UINT g_uCount4 = 0;          // 用来控制各个线程的循环计数
  15. static UINT g_uCount5 = 0;
  16. static UINT g_uCount6 = 0;
  17. static UINT g_uCount7 = 0;
  18. CProgressCtrl* g_ptrProgress1 = NULL;
  19. CProgressCtrl* g_ptrProgress2 = NULL;
  20. CProgressCtrl* g_ptrProgress3 = NULL;     //用来保存指向各个进度条的指针
  21. CProgressCtrl* g_ptrProgress4 = NULL;
  22. CProgressCtrl* g_ptrProgress5 = NULL;
  23. CProgressCtrl* g_ptrProgress6 = NULL;
  24. CProgressCtrl* g_ptrProgress7 = NULL;
  25. UINT Thread1(LPVOID)
  26. {
  27. while (g_uCount1 < 100000000 )
  28. {
  29. g_uCount1++;
  30. if (g_uCount1 % 10000000 == 0)
  31. {
  32. if (g_ptrProgress1)
  33. {
  34. g_ptrProgress1->SetPos(g_uCount1/1000000);
  35. }
  36. }
  37. }
  38. return 0;
  39. }
  40. UINT Thread2(LPVOID)
  41. {
  42. while (g_uCount2 < 100000000 )
  43. {
  44. g_uCount2++;
  45. if (g_uCount2 % 10000000 == 0)
  46. {
  47. if (g_ptrProgress2)
  48. {
  49. g_ptrProgress2->SetPos(g_uCount2/1000000);
  50. }
  51. }
  52. }
  53. return 0;
  54. }
  55. UINT Thread3(LPVOID)
  56. {
  57. while (g_uCount3 < 100000000 )
  58. {
  59. g_uCount3++;
  60. if (g_uCount3 % 10000000 == 0)
  61. {
  62. if (g_ptrProgress3)
  63. {
  64. g_ptrProgress3->SetPos(g_uCount3/1000000);
  65. }
  66. }
  67. }
  68. return 0;
  69. }
  70. UINT Thread4(LPVOID)
  71. {
  72. while (g_uCount4 < 100000000 )
  73. {
  74. g_uCount4++;
  75. if (g_uCount4 % 10000000 == 0)
  76. {
  77. if (g_ptrProgress4)
  78. {
  79. g_ptrProgress4->SetPos(g_uCount4/1000000);
  80. }
  81. }
  82. }
  83. return 0;
  84. }
  85. UINT Thread5(LPVOID)
  86. {
  87. while (g_uCount5 < 100000000 )
  88. {
  89. g_uCount5++;
  90. if (g_uCount5 % 10000000 == 0)
  91. {
  92. if (g_ptrProgress5)
  93. {
  94. g_ptrProgress5->SetPos(g_uCount5/1000000);
  95. }
  96. }
  97. }
  98. return 0;
  99. }
  100. UINT Thread6(LPVOID)
  101. {
  102. while (g_uCount6 < 100000000 )
  103. {
  104. g_uCount6++;
  105. if (g_uCount6 % 10000000 == 0)
  106. {
  107. if (g_ptrProgress6)
  108. {
  109. g_ptrProgress6->SetPos(g_uCount6/1000000);
  110. }
  111. }
  112. }
  113. return 0;
  114. }
  115. UINT Thread7(LPVOID)
  116. {
  117. while (g_uCount7 < 100000000 )
  118. {
  119. g_uCount7++;
  120. if (g_uCount7 % 10000000 == 0)
  121. {
  122. if (g_ptrProgress7)
  123. {
  124. g_ptrProgress7->SetPos(g_uCount7/1000000);
  125. }
  126. }
  127. }
  128. return 0;
  129. }
  130. /////////////////////////////////////////////////////////////////////////////
  131. // CAboutDlg dialog used for App About
  132. class CAboutDlg : public CDialog
  133. {
  134. public:
  135. CAboutDlg();
  136. // Dialog Data
  137. //{{AFX_DATA(CAboutDlg)
  138. enum { IDD = IDD_ABOUTBOX };
  139. //}}AFX_DATA
  140. // ClassWizard generated virtual function overrides
  141. //{{AFX_VIRTUAL(CAboutDlg)
  142. protected:
  143. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  144. //}}AFX_VIRTUAL
  145. // Implementation
  146. protected:
  147. //{{AFX_MSG(CAboutDlg)
  148. //}}AFX_MSG
  149. DECLARE_MESSAGE_MAP()
  150. };
  151. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  152. {
  153. //{{AFX_DATA_INIT(CAboutDlg)
  154. //}}AFX_DATA_INIT
  155. }
  156. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  157. {
  158. CDialog::DoDataExchange(pDX);
  159. //{{AFX_DATA_MAP(CAboutDlg)
  160. //}}AFX_DATA_MAP
  161. }
  162. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  163. //{{AFX_MSG_MAP(CAboutDlg)
  164. // No message handlers
  165. //}}AFX_MSG_MAP
  166. END_MESSAGE_MAP()
  167. /////////////////////////////////////////////////////////////////////////////
  168. // CThreadPriorityDlg dialog
  169. CThreadPriorityDlg::CThreadPriorityDlg(CWnd* pParent /*=NULL*/)
  170. : CDialog(CThreadPriorityDlg::IDD, pParent)
  171. {
  172. //{{AFX_DATA_INIT(CThreadPriorityDlg)
  173. // NOTE: the ClassWizard will add member initialization here
  174. //}}AFX_DATA_INIT
  175. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  176. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  177. }
  178. void CThreadPriorityDlg::DoDataExchange(CDataExchange* pDX)
  179. {
  180. CDialog::DoDataExchange(pDX);
  181. //{{AFX_DATA_MAP(CThreadPriorityDlg)
  182. // NOTE: the ClassWizard will add DDX and DDV calls here
  183. //}}AFX_DATA_MAP
  184. }
  185. BEGIN_MESSAGE_MAP(CThreadPriorityDlg, CDialog)
  186. //{{AFX_MSG_MAP(CThreadPriorityDlg)
  187. ON_WM_SYSCOMMAND()
  188. ON_WM_PAINT()
  189. ON_WM_QUERYDRAGICON()
  190. ON_BN_CLICKED(IDC_START, OnStart)
  191. //}}AFX_MSG_MAP
  192. END_MESSAGE_MAP()
  193. /////////////////////////////////////////////////////////////////////////////
  194. // CThreadPriorityDlg message handlers
  195. BOOL CThreadPriorityDlg::OnInitDialog()
  196. {
  197. CDialog::OnInitDialog();
  198. // Add "About..." menu item to system menu.
  199. // IDM_ABOUTBOX must be in the system command range.
  200. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  201. ASSERT(IDM_ABOUTBOX < 0xF000);
  202. CMenu* pSysMenu = GetSystemMenu(FALSE);
  203. if (pSysMenu != NULL)
  204. {
  205. CString strAboutMenu;
  206. strAboutMenu.LoadString(IDS_ABOUTBOX);
  207. if (!strAboutMenu.IsEmpty())
  208. {
  209. pSysMenu->AppendMenu(MF_SEPARATOR);
  210. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  211. }
  212. }
  213. // Set the icon for this dialog.  The framework does this automatically
  214. //  when the application's main window is not a dialog
  215. SetIcon(m_hIcon, TRUE); // Set big icon
  216. SetIcon(m_hIcon, FALSE); // Set small icon
  217. // TODO: Add extra initialization here
  218. g_ptrProgress1 = (CProgressCtrl*)GetDlgItem(IDC_PROGRESS1);
  219. g_ptrProgress2 = (CProgressCtrl*)GetDlgItem(IDC_PROGRESS2);
  220. g_ptrProgress3 = (CProgressCtrl*)GetDlgItem(IDC_PROGRESS3);
  221. g_ptrProgress4 = (CProgressCtrl*)GetDlgItem(IDC_PROGRESS4);
  222. g_ptrProgress5 = (CProgressCtrl*)GetDlgItem(IDC_PROGRESS5);
  223. g_ptrProgress6 = (CProgressCtrl*)GetDlgItem(IDC_PROGRESS6);
  224. g_ptrProgress7 = (CProgressCtrl*)GetDlgItem(IDC_PROGRESS7);
  225. return TRUE;  // return TRUE  unless you set the focus to a control
  226. }
  227. void CThreadPriorityDlg::OnSysCommand(UINT nID, LPARAM lParam)
  228. {
  229. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  230. {
  231. CAboutDlg dlgAbout;
  232. dlgAbout.DoModal();
  233. }
  234. else
  235. {
  236. CDialog::OnSysCommand(nID, lParam);
  237. }
  238. }
  239. // If you add a minimize button to your dialog, you will need the code below
  240. //  to draw the icon.  For MFC applications using the document/view model,
  241. //  this is automatically done for you by the framework.
  242. void CThreadPriorityDlg::OnPaint() 
  243. {
  244. if (IsIconic())
  245. {
  246. CPaintDC dc(this); // device context for painting
  247. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  248. // Center icon in client rectangle
  249. int cxIcon = GetSystemMetrics(SM_CXICON);
  250. int cyIcon = GetSystemMetrics(SM_CYICON);
  251. CRect rect;
  252. GetClientRect(&rect);
  253. int x = (rect.Width() - cxIcon + 1) / 2;
  254. int y = (rect.Height() - cyIcon + 1) / 2;
  255. // Draw the icon
  256. dc.DrawIcon(x, y, m_hIcon);
  257. }
  258. else
  259. {
  260. CDialog::OnPaint();
  261. }
  262. }
  263. // The system calls this to obtain the cursor to display while the user drags
  264. //  the minimized window.
  265. HCURSOR CThreadPriorityDlg::OnQueryDragIcon()
  266. {
  267. return (HCURSOR) m_hIcon;
  268. }
  269. void CThreadPriorityDlg::OnStart() 
  270. {
  271. //创建7个不同优先级别的线程
  272. AfxBeginThread(Thread1,NULL,THREAD_PRIORITY_TIME_CRITICAL);
  273. AfxBeginThread(Thread2,NULL,THREAD_PRIORITY_HIGHEST);
  274. AfxBeginThread(Thread3,NULL,THREAD_PRIORITY_ABOVE_NORMAL);
  275. AfxBeginThread(Thread4,NULL,THREAD_PRIORITY_NORMAL);
  276. AfxBeginThread(Thread5,NULL,THREAD_PRIORITY_BELOW_NORMAL);
  277. AfxBeginThread(Thread6,NULL,THREAD_PRIORITY_LOWEST);
  278. AfxBeginThread(Thread7,NULL,THREAD_PRIORITY_IDLE);
  279. }