Visual C++ Program for the Implementation of Thread Control | CS1255 - Visual Programming Laboratory


AIM:
To write a Visual C++ Program for the Implementation of Thread Control in CS1255 - Visual Programming Lab.

SOURCE CODE:
Progress.h:
#if !defined(AFX_PROGRESS_H__3566042B_6A01_4310_BA14_2478EC06E340__INCLUDED_)
#define AFX_PROGRESS_H__3566042B_6A01_4310_BA14_2478EC06E340__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// Progress.h : header file
//
// Progress dialog

class Progress : public CDialog
{
// Construction
public:
    Progress(CWnd* pParent = NULL);   // standard constructor

// Dialog Data
    //{{AFX_DATA(Progress)
    enum { IDD = IDD_DIALOG1 };
        // NOTE: the ClassWizard will add data members here
    //}}AFX_DATA
// Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(Progress)
    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    //}}AFX_VIRTUAL

// Implementation
protected:

    // Generated message map functions
    //{{AFX_MSG(Progress)
    afx_msg void OnExit();
    virtual void OnCancel();
    afx_msg void OnStart();
    afx_msg void OnTimer(UINT nIDEvent);
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
private:
    int m_nTimer;
    int m_nCount;
    enum { nMaxCount=10000 };
};

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

#endif // !defined(AFX_PROGRESS_H__3566042B_6A01_4310_BA14_2478EC06E340__INCLUDED_)

Progress.cpp: implementation file
#include "stdafx.h"
#include "Thread.h"
#include "Progress.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// Progress dialog

Progress::Progress(CWnd* pParent /*=NULL*/)
    : CDialog(Progress::IDD, pParent)
{
    m_nCount=0;
    //{{AFX_DATA_INIT(Progress)
        // NOTE: the ClassWizard will add member initialization here
    //}}AFX_DATA_INIT
}

void Progress::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(Progress)
        // NOTE: the ClassWizard will add DDX and DDV calls here
    //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(Progress, CDialog)
    //{{AFX_MSG_MAP(Progress)
    ON_BN_CLICKED(IDEXIT, OnExit)
    ON_BN_CLICKED(IDC_START, OnStart)
    ON_WM_TIMER()
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

// Progress message handlers

void Progress::OnCancel()
{
    m_nCount=0;
    if(m_nCount== 0)
        GetDlgItem(IDC_START)->EnableWindow(TRUE);
}

void Progress::OnTimer(UINT nIDEvent)
{
    CProgressCtrl* pBar=(CProgressCtrl*)GetDlgItem(IDC_PROGRESS1);
    pBar->SetPos(m_nCount*100/nMaxCount);
    CDialog::OnTimer(nIDEvent);
}

void Progress::OnStart()
{
    MSG message;
    m_nTimer=SetTimer(1,100,NULL);
    ASSERT(m_nTimer!=0);
    GetDlgItem(IDC_START)->EnableWindow(FALSE);
    volatile int nTemp;
    for(m_nCount=0;m_nCount<nMaxCount;m_nCount++)
    {
        for(nTemp=0;nTemp<100000;nTemp++)
        {
        }
        if(::PeekMessage(&message,NULL,0,0,PM_REMOVE))
        {
            ::TranslateMessage(&message);
            ::DispatchMessage(&message);
        }
    }
}

ThreadView.cpp:
// CThreadView message handlers
void CThreadView::OnLButtonDown(UINT nFlags, CPoint point)
{
    Progress p;
    p.DoModal();
    CView::OnLButtonDown(nFlags, point);
}

OUTPUT:
Previous
Next Post »

Still not found what you are looking for? Try again here.