THREAD | TO CREATE AND IMPLEMENT THE USE OF THEREAD IN VC++

OBJECTIVE:

To implement the use of threads in vc++.

PROCEDURE:

1. Run the MFC appwizard(dll) to generate the application c:\thread

2. Select the ingle document in step 1 of appwizard

3. Select all the default settings except printing and print preview

4. Using the resource view, right click on the dialog and select the new dialog

5. Create new dialog box named IDC_PROGRESS1

6. Name the new dialog on IDD_COMPUTE using properties

7. The default OK button can be changed to IDC_START

8. Leave the default cancel button as such

9. By clicking on the dialog IDD_COMPUTE the system will automatically ask for a class name ive the classname as CComputeDlg

10. Using the class wizard map the message BN_CLICKED to the IDC_START, IDC_CANCEL objects

11. Declare the following in the compute DLG.H file next to #ENDIF

#define WM_THREADFINISHED

class CComputeDlg : public CDialog

{

LRESULT OnThreadFinished(WPARAM wparam,LPARAM lparam);

private:

int m_nTimer;

public:

enum{nMaxCount=10000};

12. Using class wizard map the message WM_TIMER to the CComputeDlg.cpp files as follows

void CComputeDlg::OnTimer(UINT nIDEvent)

{

CProgressCtrl*pBar=(CProgressCtrl*)GetDlgItem(IDC_PROGRESS1);

pBar->SetPos(g_nCount*100/nMaxCount);

CDialog::OnTimer(nIDEvent);

}

13. Type the following in compute Dlg.cpp type the definition of the thread before the constructor next to #endif

int g_nCount=0;

UINT ComputeThreadProc(LPVOID pParam)

{

volatile int nTemp;

for(g_nCount=0;g_nCount
EnableWindow(FALSE);

AfxBeginThread(ComputeThreadProc,GetSafeHwnd(),THREAD_PRIORITY_NORMAL);

}

void CComputeDlg::OnCancel()

{

if(g_nCount==0)

{

CDialog::OnCancel();

}

else

{

g_nCount=nMaxCount;

}

}

15. Type the OnThreadFinished definition in the end of ComputeDlg.cpp file

LRESULT CComputeDlg::OnThreadFinished(WPARAM wparam,LPARAM lparam)

{

CDialog::OnOK();

return 0;

}

16. Using the class wizard map the CThreadView class an edit the following

void CThreadView::OnLButtonDown(UINT nFlags, CPoint point)

{

dlg.DoModal();

CView::OnLButtonDown(nFlags, point);

}

17. And in OnDraw function of CThreadView.cpp type the following

pDC->TextOut(0,0,"press the left mouse button here");

18. Include the header file of ComputeDlg in CThreadView.cpp

#include "ComputeDlg.h"

19. Compile and run and test the application



PROGRAM:

SOURCE FILES:

Computedlg.cpp :

#include "stdafx.h"

#include "thread.h"

#include "ComputeDlg.h"



#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[] = __FILE__;

#endif

/////////////////////////////////////////////////////////////////////////////

// CComputeDlg dialog

int g_nCount=0;

UINT ComputeThreadProc(LPVOID pParam)

{

volatile int nTemp;

for(g_nCount=0;g_nCount
EnableWindow(FALSE);

AfxBeginThread(ComputeThreadProc,GetSafeHwnd(),THREAD_PRIORITY_NORMAL);

}

void CComputeDlg::OnCancel()

{

// TODO: Add extra cleanup here

if(g_nCount==0)

{

CDialog::OnCancel();

}

else

{

g_nCount=nMaxCount;

}

}

void CComputeDlg::OnTimer(UINT nIDEvent)

{

// TODO: Add your message handler code here and/or call default

CProgressCtrl*pBar=(CProgressCtrl*)GetDlgItem(IDC_PROGRESS1);

pBar->SetPos(g_nCount*100/nMaxCount);

CDialog::OnTimer(nIDEvent);

}

LRESULT CComputeDlg::OnThreadFinished(WPARAM wparam,LPARAM lparam)

{

CDialog::OnOK();

return 0;

}

Threadview.cpp :

// threadView.cpp : implementation of the CThreadView class

#include "stdafx.h"

#include "thread.h"

#include "threadDoc.h"

#include "threadView.h"

#include "ComputeDlg.h"

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[] = __FILE__;

#endif

/////////////////////////////////////////////////////////////////////////////

// CThreadView

IMPLEMENT_DYNCREATE(CThreadView, CView)



BEGIN_MESSAGE_MAP(CThreadView, CView)

//{{AFX_MSG_MAP(CThreadView)

ON_WM_LBUTTONDOWN()

//}}AFX_MSG_MAP

END_MESSAGE_MAP()



/////////////////////////////////////////////////////////////////////////////

// CThreadView construction/destruction



CThreadView::CThreadView()

{



}

CThreadView::~CThreadView()

{

}

BOOL CThreadView::PreCreateWindow(CREATESTRUCT& cs)

{

// TODO: Modify the Window class or styles here by modifying

// the CREATESTRUCT cs



return CView::PreCreateWindow(cs);

}

/////////////////////////////////////////////////////////////////////////////

// CThreadView drawing



void CThreadView::OnDraw(CDC* pDC)

{

CThreadDoc* pDoc = GetDocument();

ASSERT_VALID(pDoc);

// TODO: add draw code for native data here

pDC->TextOut(0,0,"press the left mouse button here");

}

/////////////////////////////////////////////////////////////////////////////

// CThreadView diagnostics

#ifdef _DEBUG

void CThreadView::AssertValid() const

{

CView::AssertValid();

}



void CThreadView::Dump(CDumpContext& dc) const

{

CView::Dump(dc);

}

CThreadDoc* CThreadView::GetDocument() // non-debug version is inline

{

ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CThreadDoc)));

return (CThreadDoc*)m_pDocument;

}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////

// CThreadView message handlers

void CThreadView::OnLButtonDown(UINT nFlags, CPoint point)

{

dlg.DoModal();

CView::OnLButtonDown(nFlags, point);

}

HEADER FILES:



Computedlg.h :

#if !defined(AFX_COMPUTEDLG_H__62A874DD_70BF_4129_BE3F_1BD11DCF341D__INCLUDED_)

#define AFX_COMPUTEDLG_H__62A874DD_70BF_4129_BE3F_1BD11DCF341D__INCLUDED_



#if _MSC_VER > 1000

#pragma once

#endif // _MSC_VER > 1000

// ComputeDlg.h : header file

//

#define WM_THREADFINISHED WM_USER +5

UINT ComputeThreadProc(LPVOID pParam);

/////////////////////////////////////////////////////////////////////////////

// CComputeDlg dialog

class CComputeDlg : public CDialog

{

// Construction

LRESULT OnThreadFinished(WPARAM wparam,LPARAM lparam);

private:

int m_nTimer;

public:

enum{nMaxCount=10000};

CComputeDlg(CWnd* pParent = NULL); // standard constructor

// Dialog Data

//{{AFX_DATA(CComputeDlg)

enum { IDD = IDD_COMPUTE };

// NOTE: the ClassWizard will add data members here

//}}AFX_DATA

// Overrides

// ClassWizard generated virtual function overrides

//{{AFX_VIRTUAL(CComputeDlg)

protected:

virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support

//}}AFX_VIRTUAL

protected:



// Generated message map functions

//{{AFX_MSG(CComputeDlg)

afx_msg void OnStart();

virtual void OnCancel();

afx_msg void OnTimer(UINT nIDEvent);

//}}AFX_MSG

DECLARE_MESSAGE_MAP()

};

//{{AFX_INSERT_LOCATION}}

// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_COMPUTEDLG_H__62A874DD_70BF_4129_BE3F_1BD11DCF341D__INCLUDED_)

OUTPUT




THREAD  TO CREATE AND IMPLEMENT THE USE OF THEREAD IN VC++







Post a Comment

0 Comments