Commit before breaking everything
[matches/honours.git] / research / transmission_spectroscopy / TOF / Win32++ / samples / Threads / src / MainWnd.cpp
1 //////////////////////////////////////////////\r
2 // MainWnd.cpp\r
3 //  Definitions for the CMainWindow class\r
4 \r
5 #include "stdafx.h"\r
6 #include "MainWnd.h"\r
7 \r
8 \r
9 CMainWindow::CMainWindow() : m_nWindowsCreated(0)\r
10 {\r
11         // Set the number of threads\r
12         m_nTestWin = 20;\r
13 \r
14         // A couple of notes in case you're tempted to test how many threads with test windows can be created ...\r
15 \r
16         // Note 1: A Windows limit of 10000 handles per process imposes a practical limit of aprox 1000 test windows.\r
17         //         Refer to: http://support.microsoft.com/kb/327699\r
18         // Note 2: All our threads belong to the one process.\r
19         // Note 3: Creating (or destroying) more than say 200 windows may temporarily stress the Explorer process.\r
20         // Note 4: This sample is intended as "proof of concept" only. A well written program should not require 20 GUI threads!\r
21 }\r
22 \r
23 HWND CMainWindow::Create(CWnd* pParent)\r
24 {\r
25         CString str = _T("Main Thread Window");\r
26 \r
27         // Create the main window\r
28         return CreateEx(WS_EX_TOPMOST, NULL, str, WS_OVERLAPPEDWINDOW | WS_VISIBLE,\r
29                 20 , 50, 400, 300, pParent, NULL);\r
30 }\r
31 \r
32 void CMainWindow::OnCreate()\r
33 {\r
34         // Create each CMyThread object\r
35         for (int i = 1 ; i <= m_nTestWin ; i++)\r
36         {\r
37                 // Create the Test Window and store the CTestWindow pointer\r
38                 CTestWindow* pTestWin = new CTestWindow(i);\r
39 \r
40                 CString str;\r
41                 str.Format( _T("Thread %d started\n"), i );\r
42                 TRACE(str);\r
43 \r
44                 m_vTestWnd.push_back(pTestWin);\r
45         }\r
46 \r
47         std::vector<TestWndPtr>::iterator iter;\r
48         for (iter = m_vTestWnd.begin(); iter < m_vTestWnd.end(); ++iter)\r
49         {\r
50                 (*iter)->ResumeThread();\r
51         }\r
52 }\r
53 \r
54 void CMainWindow::OnAllWindowsCreated()\r
55 {\r
56         CString str;\r
57         str.Format( _T("%d Test windows created in seperate threads\n"), m_nTestWin );\r
58         TRACE(str);\r
59 }\r
60 \r
61 LRESULT CMainWindow::WndProc(UINT uMsg, WPARAM wParam, LPARAM lParam)\r
62 {\r
63         switch (uMsg)\r
64         {\r
65         case WM_COMMAND:\r
66                 if (HIWORD(wParam) == EN_SETFOCUS)\r
67                         SetFocus();\r
68                 break;\r
69 \r
70         case WM_CLOSE:\r
71                 {\r
72                         // Close each thread window.\r
73                         // The thread is then terminated with a WM_QUIT when its window is destroyed.\r
74                         std::vector<TestWndPtr>::iterator iter;\r
75                         for (iter = m_vTestWnd.begin(); iter < m_vTestWnd.end(); ++iter)\r
76                         {\r
77                                 if ((*iter)->IsWindow())\r
78                                         (*iter)->SendMessage(WM_CLOSE, 0, 0);\r
79                         }\r
80                 }\r
81                 break;\r
82 \r
83         case WM_DESTROY:\r
84                 {\r
85                         // Terminate the primary thread.\r
86                         ::PostQuitMessage(0);\r
87                 }\r
88                 break;\r
89 \r
90         case WM_WINDOWCREATED:\r
91                 {\r
92                         // Message recieved when a test window is created\r
93                         CString str;\r
94                         ++m_nWindowsCreated;\r
95                         str.Format( _T("Created Window %d\n"), m_nWindowsCreated );\r
96                         TRACE(str);\r
97                         if (m_nWindowsCreated == m_nTestWin)\r
98                                 OnAllWindowsCreated();\r
99                 }\r
100                 break;\r
101         }\r
102 \r
103         return WndProcDefault(uMsg, wParam, lParam);\r
104 }\r
105 \r

UCC git Repository :: git.ucc.asn.au