///////////////////////////////////////////////////////////////////////////// // dialogDemoDlg.cpp - demonstrate use of UI and worker threads // // Jim Fawcett, CSE691 - Software Modeling and Analysis, Fall 2000 ///////////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "dialogDemo.h" #include "dialogDemoDlg.h" #include #include #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: //{{AFX_MSG(CAboutDlg) //}}AFX_MSG DECLARE_MESSAGE_MAP() }; //----< about box constructor >-------------------------------------- CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT } // //----< about box DDX function >------------------------------------- void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP } //----< message map for about box >---------------------------------- BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // global data HWND hWnd; // handle to main dialog, used by worker thread to post // windows messages to main thread. It is initialized // in CDialogDemoDlg::OnInitDialog(); ///////////////////////////////////////////////////////////////////////////// // user defined messages - used by worker thread to tell main thread // what processing it needs main thread to carry out #define WM_USER_SETTEXT WM_USER + 0X200 #define WM_USER_SETCOUNT WM_USER + 0X201 #define WM_USER_SETITER WM_USER + 0X202 #define WM_USER_SETFOCUS WM_USER + 0X203 // ///////////////////////////////////////////////////////////////////////////// // Global work functions bool Stop = false; //----< simulate an intense piece of work >-------------------------- void doWork() { double temp = 1.0/3.0; int i, IMax=5000; for(i=0; i-------------------------- // // a function executed on the User Interface's main thread // can safely use pointers to the interface's windows, as // done here. That is NOT SAFE for functions called by threads // that did not create the window, e.g., worker threads. // // Notes: // - this is a global function so it needs a pointer to the // main dialog window to call its functions. // - this function is called by a member function of the main // dialog class, so the member function will simply pass its // "this" pointer. // void doWorkMainThread(int count, CWnd *pDlg) { Stop = false; pDlg->SetWindowText("Main thread running"); int i; for(i=0; iSetDlgItemInt(IDC_EDIT2,i); doWork(); } pDlg->SetDlgItemInt(IDC_EDIT2,i); CWnd *pWnd = pDlg->GetDlgItem(IDC_EDIT1); pWnd->SetFocus(); Stop = true; pDlg->SetWindowText("Main thread stopped"); } // //----< run work simulator on worker thread >------------------------ // // This is a global function, called by a worker thread, e.g., by a // thread that did not create any of the windows in the interface. // It is NOT SAFE for a worker thread to access a window's functions // through a pointer to the window, as was done in the function above. // // What will work is to have the worker thread send or post a message // to the recieving window, as illustrated here. // void _cdecl doWorkWorkerThread(void *pV) { Stop = false; int count = *((int*)pV); ::SendMessage(hWnd,WM_USER_SETTEXT,(WPARAM)0,(LPARAM)"Worker thread running"); int i; for(i=0; i------------------------------------------------ CDialogDemoDlg::CDialogDemoDlg(CWnd* pParent /*=NULL*/) : CDialog(CDialogDemoDlg::IDD, pParent) { //{{AFX_DATA_INIT(CDialogDemoDlg) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } //----< DDX function, not used by this program >--------------------- void CDialogDemoDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CDialogDemoDlg) // NOTE: the ClassWizard will add DDX and DDV calls here //}}AFX_DATA_MAP } //----< dialog application's message map >--------------------------- BEGIN_MESSAGE_MAP(CDialogDemoDlg, CDialog) //{{AFX_MSG_MAP(CDialogDemoDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_EN_CHANGE(IDC_EDIT1, OnChangeEdit1) ON_BN_CLICKED(IDC_RADIO1, OnNo) ON_BN_CLICKED(IDC_RADIO2, OnYes) ON_BN_CLICKED(IDCSTOP, OnStop) ON_MESSAGE(WM_USER_SETTEXT, OnSetText ) ON_MESSAGE(WM_USER_SETCOUNT, OnSetCount) ON_MESSAGE(WM_USER_SETITER, OnSetIter ) ON_MESSAGE(WM_USER_SETFOCUS, OnSetFocus) ON_BN_CLICKED(IDC_RADIO3, OnHighest) ON_BN_CLICKED(IDC_RADIO4, OnNormal) ON_BN_CLICKED(IDC_RADIO5, OnLowest) //}}AFX_MSG_MAP END_MESSAGE_MAP() // ///////////////////////////////////////////////////////////////////////////// // CDialogDemoDlg message handlers //----< initialize dialog >------------------------------------------ BOOL CDialogDemoDlg::OnInitDialog() { CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // save handle to this main dialog in global variable, hWnd hWnd = m_hWnd; // set workerThread true radio button CButton *pRB = (CButton*)GetDlgItem(IDC_RADIO2); pRB->SetCheck(1); workerThread = true; // set workerThread Priority Normal button pRB = (CButton*)GetDlgItem(IDC_RADIO4); pRB->SetCheck(1); workerThreadPriority = Normal; // set default number of times to call work piece SetDlgItemInt(IDC_EDIT1,10000); SetDlgItemText(IDC_EDIT2,""); // set focus to user input edit box CWnd *pEdit; pEdit = GetDlgItem(IDC_EDIT1); pEdit->SetFocus(); Stop = true; return FALSE; // return TRUE unless you set the focus to a control } // //----< system menu handler >---------------------------------------- void CDialogDemoDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } } //----< draw icon and set title font >------------------------------- // If you add a minimize button to your dialog, you will need the code below // to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework. void CDialogDemoDlg::OnPaint() { // modify font for title CFont font; font.CreateFont( 16,0,0,0,FW_DEMIBOLD,0,0,0,ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, _T("Times Roman") ); CStatic *pSt = (CStatic*)GetDlgItem(IDC_STATIC_0); pSt->SetFont(&font); if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } } // //----< get cursor >------------------------------------------------- // The system calls this to obtain the cursor to display while the user drags // the minimized window. HCURSOR CDialogDemoDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } //----< get count from Edit1, reset Edit2 >-------------------------- void CDialogDemoDlg::OnChangeEdit1() { count = GetDlgItemInt(IDC_EDIT1); SetDlgItemInt(IDC_EDIT2,0); } //----< set No radio button, reset Yes radio button >---------------- void CDialogDemoDlg::OnNo() { CButton *pRB = (CButton*)GetDlgItem(IDC_RADIO1); pRB->SetCheck(1); workerThread = false; pRB = (CButton*)GetDlgItem(IDC_RADIO2); pRB->SetCheck(0); CWnd *pEdit = GetDlgItem(IDC_EDIT1); pEdit->SetFocus(); } //----< set Yes radio button, reset No radio button >---------------- void CDialogDemoDlg::OnYes() { CButton *pRB = (CButton*)GetDlgItem(IDC_RADIO2); pRB->SetCheck(1); workerThread = true; pRB = (CButton*)GetDlgItem(IDC_RADIO1); pRB->SetCheck(0); CWnd *pEdit = GetDlgItem(IDC_EDIT1); pEdit->SetFocus(); } // //----< set worker thread priority to highest >---------------------- void CDialogDemoDlg::OnHighest() { CButton *pRB = (CButton*)GetDlgItem(IDC_RADIO3); pRB->SetCheck(1); workerThreadPriority = Highest; CWnd *pEdit = GetDlgItem(IDC_EDIT1); pEdit->SetFocus(); } //----< set worker thread priority to normal >----------------------- void CDialogDemoDlg::OnNormal() { CButton *pRB = (CButton*)GetDlgItem(IDC_RADIO4); pRB->SetCheck(1); workerThreadPriority = Normal; CWnd *pEdit = GetDlgItem(IDC_EDIT1); pEdit->SetFocus(); } //----< set worker thread priority to lowest >----------------------- void CDialogDemoDlg::OnLowest() { CButton *pRB = (CButton*)GetDlgItem(IDC_RADIO5); pRB->SetCheck(1); workerThreadPriority = Lowest; CWnd *pEdit = GetDlgItem(IDC_EDIT1); pEdit->SetFocus(); } // //----< do work specified number of times >-------------------------- // // run on main thread or worker thread based on radio button settings // // Worker thread is spawned here, by a call to _beginthread. For this // to work, you need to ensure two things: // - you #include - see top of this file // - you tell project settings you need to use multi-threaded libraries. // You do that by: // Project/Settings/C\C++ tab/category: code generation/ // Use Run-time Library: Debug Multithreaded // this is a default setting for MFC projects. // void CDialogDemoDlg::OnOK() { if(!Stop) return; if(workerThread == false) doWorkMainThread(count,this); else { HANDLE hThrd = (HANDLE)_beginthread(doWorkWorkerThread,0,(void*)&count); switch(workerThreadPriority) { case Highest : SetThreadPriority(hThrd,THREAD_PRIORITY_HIGHEST); break; case Normal : SetThreadPriority(hThrd,THREAD_PRIORITY_NORMAL); break; case Lowest : SetThreadPriority(hThrd,THREAD_PRIORITY_LOWEST); break; } } Stop = true; } // //----< set global stop signal >------------------------------------- void CDialogDemoDlg::OnStop() { Stop = true; } //----< set text in window caption >--------------------------------- LRESULT CDialogDemoDlg::OnSetText(WPARAM wParam, LPARAM lParam) { SetWindowText((LPCTSTR)lParam); return 1; } //----< set count in Edit1 >----------------------------------------- LRESULT CDialogDemoDlg::OnSetCount(WPARAM wParam, LPARAM lParam) { SetDlgItemInt(IDC_EDIT1,(int)lParam); return 1; } //----< set iteration count in Edit2 >------------------------------- LRESULT CDialogDemoDlg::OnSetIter(WPARAM wParam, LPARAM lParam) { SetDlgItemInt(IDC_EDIT2, (int)lParam); return 1; } //----< set focus to Edit1 >----------------------------------------- LRESULT CDialogDemoDlg::OnSetFocus(WPARAM wParam, LPARAM lParam) { CWnd *pWnd = GetDlgItem(IDC_EDIT1); pWnd->SetFocus(); return 1; }