1 //////////////////////////////////////////////
\r
3 // Definitions for the CMainWindow class
\r
9 CMainWindow::CMainWindow() : m_nWindowsCreated(0)
\r
11 // Set the number of threads
\r
14 // A couple of notes in case you're tempted to test how many threads with test windows can be created ...
\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
23 HWND CMainWindow::Create(CWnd* pParent)
\r
25 CString str = _T("Main Thread Window");
\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
32 void CMainWindow::OnCreate()
\r
34 // Create each CMyThread object
\r
35 for (int i = 1 ; i <= m_nTestWin ; i++)
\r
37 // Create the Test Window and store the CTestWindow pointer
\r
38 CTestWindow* pTestWin = new CTestWindow(i);
\r
41 str.Format( _T("Thread %d started\n"), i );
\r
44 m_vTestWnd.push_back(pTestWin);
\r
47 std::vector<TestWndPtr>::iterator iter;
\r
48 for (iter = m_vTestWnd.begin(); iter < m_vTestWnd.end(); ++iter)
\r
50 (*iter)->ResumeThread();
\r
54 void CMainWindow::OnAllWindowsCreated()
\r
57 str.Format( _T("%d Test windows created in seperate threads\n"), m_nTestWin );
\r
61 LRESULT CMainWindow::WndProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
\r
66 if (HIWORD(wParam) == EN_SETFOCUS)
\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
77 if ((*iter)->IsWindow())
\r
78 (*iter)->SendMessage(WM_CLOSE, 0, 0);
\r
85 // Terminate the primary thread.
\r
86 ::PostQuitMessage(0);
\r
90 case WM_WINDOWCREATED:
\r
92 // Message recieved when a test window is created
\r
94 ++m_nWindowsCreated;
\r
95 str.Format( _T("Created Window %d\n"), m_nWindowsCreated );
\r
97 if (m_nWindowsCreated == m_nTestWin)
\r
98 OnAllWindowsCreated();
\r
103 return WndProcDefault(uMsg, wParam, lParam);
\r