EQuestionAnswers.com Computer/Electronics Questions and Answers
C, C++, VC++, COM/DCOM, DLL and more
#Login #Sign up  Facebook Twitter TGoogle+
 

#Previous question #Index of VC++/WIN32/MFC Questions #Next question
#include <afxwin.h>
#define ID_WORKER 1001
class CMyWnd : public CFrameWnd
{
private:
  CButton m_BWorker;
  CWinThread *m_pThread;
10    bool m_bThread;
11    friend  UINT WorkerThread(LPVOID arg);
12  public:
13    CMyWnd()
14    {
15      
16      CRect rt;
17      rt.top = 10;
18      rt.left = 150;
19      rt.right = 250;
20      rt.bottom = 30;
21      Create(NULL, _T("MFC Worker Thread"));
22      m_BWorker.Create("Worker", WS_CHILD|WS_VISIBLE, rt, this, ID_WORKER);
23      m_pThread = NULL;
24      m_bThread = TRUE;
25    }
26 
27    afx_msg void OnWorker();
28 
29    DECLARE_MESSAGE_MAP()
30  };
31  BEGIN_MESSAGE_MAP(CMyWnd, CFrameWnd)
32  ON_COMMAND(ID_WORKER, OnWorker)
33  END_MESSAGE_MAP()
34 
35  UINT WorkerThread(LPVOID arg)
36  {
37     SYSTEMTIME st;
38     CMyWnd * pWnd;
39     CMyWnd *pWnd = (CMyWnd *)arg;
40     while(pWnd->m_bThread){
41     GetSystemTime(&st);
42     CString tm;
43     tm.Format("Time %.2d:%.2d:%.2d Date %.2d/%.2d/%.4d",
44               st.wHour, st.wMinute, st.wSecond, st.wDay, st.wMonth, st.wYear);
45     pWnd->SetWindowText((LPCTSTR)tm);
46     Sleep(500);
47     }
48     AfxEndThread(0, TRUE);
49     return 0;
50  }
51  afx_msg void CMyWnd::OnWorker()
52  {
53    if(!m_pThread) {
54      m_bThread = TRUE;
55      m_pThread = AfxBeginThread(WorkerThread,this,THREAD_PRIORITY_NORMAL,0,0,NULL);
56    } else {
57      m_bThread = FALSE;
58      m_pThread = NULL;
59    }
60  }
61 
62  class CMyApp : public  CWinApp 
63  {
64 
65  public:
66    virtual BOOL InitInstance()
67    {
68 
69      m_pMainWnd = new CMyWnd();
70      m_pMainWnd->ShowWindow(SW_SHOW);
71      m_pMainWnd->UpdateWindow();
72 
73         return TRUE;
74    }
75   };
76 
77 
78 
79   CMyApp app;

MFC Worker Thread Demo

You have viewed 1 page out of 109. Your VC++ learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

#Programming Windows, Fifth Edition (Developer Reference)
#Programming Applications for Microsoft Windows (Microsoft Programming Series)
#Programming Windows with MFC, Second Edition
#Visual C++ Programming
#Programming Microsoft Visual C++, Fifth Edition

* #1 webmaster Sat 26 Apr/2014 15:15:32(GMT)  Like 0 Unlike 0

Dear Users,

We are pleased to inform you that a forum/blog has been incorporated with www.mybestnotes.co.in. You are welcomed to add your comments, requests, codes, solutions and feedback to it. Please login(if already a member) or signup(for free) to avail to this facility.

Regards,
Webmaster

Your message goes here:

Name:Guest
Email:anonymous@unknown.com
My Post:*
Secutiry Code: ******  *
Preview this compose before posting this in discussion forum.
 
Note:
  1. Use [Sxx] code to show smiles. Example [S02]
  2. Use [URL ], example [URL http://www.google.com/]
  3. To display code or un formatted text use [CODE] [/CODE]. Example: [CODE] printf("Hello world"); [/CODE]
 [S01]   [S02]   [S03]   [S04]   [S05]   [S06]   [S07]   [S08] 
 [S09]   [S10]   [S11]   [S12]   [S13]   [S14]   [S15]   [S16] 
 [S17]   [S18]   [S19]   [S20]   [S21]   [S22]   [S23]   [S24] 

Note: Only members are allowed, * fields are mandatory.