⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mtstestdlg.cpp

📁 《VC++ 编程技巧与示例 .rar》各个示例代码绝对可用
💻 CPP
字号:
// MTSTestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MTSTest.h"
#include "MTSTestDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

////////////////////////////////////////////////////////////////////////
//●:
#include <afxmt.h>
//全局变量:
static CCriticalSection g_Cs;
static CMutex g_Mt;
static CSemaphore g_Sp(1,1);

static UINT Data=0;
static UINT OldData=0;
static UINT cnt=0;
static HWND hDlg;
static CEvent g_EndTh1;
static CEvent g_EndTh2;
static UINT g_Sel=0;
static BOOL g_Th1End=TRUE;
static BOOL g_Th2End=TRUE;

////////////////////////////////////////////////////////////////////////
//全局函数:
static void SetData(UINT dat)
{
	CString str;
	if(g_Sel==0)//使用临界区;
	{
		g_Cs.Lock();
		Data=dat;
		str.Format("%d",Data);
		//阻塞线程,测试临界区的功效:
		::SetDlgItemText(hDlg,IDC_VALUE_SET,str);
		::Sleep(1000);
		g_Cs.Unlock();
	}
	if(g_Sel==1)//使用互斥区; 
	{
		CSingleLock singleLock(&g_Mt);
		singleLock.Lock();
		Data=dat;
		str.Format("%d",Data);
		//阻塞线程,测试临界区的功效:
		::SetDlgItemText(hDlg,IDC_VALUE_SET,str);
		::Sleep(1000);
	}
	if(g_Sel==2)//使用信号;
	{
		CSingleLock singleLock(&g_Sp);
		singleLock.Lock();
		Data=dat;
		str.Format("%d",Data);
		//阻塞线程,测试临界区的功效:
		::SetDlgItemText(hDlg,IDC_VALUE_SET,str);
		::Sleep(1000);
	}
}

static UINT GetData()
{
	UINT re;
	if(g_Sel==0)//使用临界区;
	{
		g_Cs.Lock();
		re=Data;
		g_Cs.Unlock();
	}
	if(g_Sel==1)//使用互斥区;
	{
		CSingleLock singleLock(&g_Mt);
		singleLock.Lock();
		re=Data;
	}
	if(g_Sel==2)//使用信号
	{
		CSingleLock singleLock(&g_Sp);
		singleLock.Lock();
		re=Data;
	}
		return re;
}

UINT ThreadProcWt(LPVOID param)
{
	while(1)
	{
		for(cnt=0;cnt<10;cnt++)
		{
			SetData(cnt);
			if(::WaitForSingleObject(g_EndTh1,0)==WAIT_OBJECT_0)
			{
				Data=0;
				cnt=0;
				OldData=0;
				return 0;
			}
		}
	}
	return 0;
}

UINT ThreadProcRd(LPVOID param)
{
	UINT re;
	while(1)
	{
		re=GetData();
		if(re!=OldData)
		{
			CString str;
			str.Format("%d",re);
			::SetDlgItemText(hDlg,IDC_VALUE,str);
			OldData=re;
		}
		if(::WaitForSingleObject(g_EndTh2,0)==WAIT_OBJECT_0)
		{
			return 0;
		}
	}
	return 0;
}

/////////////////////////////////////////////////////////////////////////////
// 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()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMTSTestDlg dialog

CMTSTestDlg::CMTSTestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMTSTestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMTSTestDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMTSTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMTSTestDlg)
	DDX_Control(pDX, IDC_END_THREAD2, m_EndTh2);
	DDX_Control(pDX, IDC_END_THREAD1, m_EndTh1);
	DDX_Control(pDX, IDC_THREAD2, m_BeginTh2);
	DDX_Control(pDX, IDC_THREAD1, m_BeginTh1);
	DDX_Control(pDX, IDC_RADIO_CRITICAL, m_Rd_Cs);
	DDX_Control(pDX, IDC_RADIO_MUTEX, m_Rd_Mt);
	DDX_Control(pDX, IDC_RADIO_SEMAPHORES, m_Rd_Sp);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMTSTestDlg, CDialog)
	//{{AFX_MSG_MAP(CMTSTestDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_THREAD1, OnThread1)
	ON_BN_CLICKED(IDC_THREAD2, OnThread2)
	ON_BN_CLICKED(IDC_END_THREAD1, OnEndThread1)
	ON_BN_CLICKED(IDC_END_THREAD2, OnEndThread2)
	ON_BN_CLICKED(IDC_RADIO_CRITICAL, OnRadioCritical)
	ON_BN_CLICKED(IDC_RADIO_MUTEX, OnRadioMutex)
	ON_BN_CLICKED(IDC_RADIO_SEMAPHORES, OnRadioSemaphores)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMTSTestDlg message handlers

BOOL CMTSTestDlg::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
	
	// TODO: Add extra initialization here
	//●:
	hDlg=GetSafeHwnd();
	m_EndTh1.EnableWindow(FALSE);
	m_EndTh2.EnableWindow(FALSE);
	m_Rd_Cs.SetCheck(1);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMTSTestDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 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 CMTSTestDlg::OnPaint() 
{
	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();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMTSTestDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//●:

void CMTSTestDlg::OnThread1() 
{
	// TODO: Add your control notification handler code here
	m_BeginTh1.EnableWindow(FALSE);
	m_EndTh1.EnableWindow(TRUE);
	g_Th1End=FALSE;
	EnableSelections(FALSE);
	g_EndTh1.ResetEvent();
	AfxBeginThread(ThreadProcWt,GetSafeHwnd());
}

void CMTSTestDlg::OnEndThread1() 
{
	// TODO: Add your control notification handler code here
	m_BeginTh1.EnableWindow(TRUE);
	m_EndTh1.EnableWindow(FALSE);
	g_Th1End=TRUE;
	EnableSelections();
	g_EndTh1.SetEvent();
}

void CMTSTestDlg::OnThread2() 
{
	// TODO: Add your control notification handler code here
	m_BeginTh2.EnableWindow(FALSE);
	m_EndTh2.EnableWindow(TRUE);
	g_Th2End=FALSE;
	EnableSelections(FALSE);
	g_EndTh2.ResetEvent();
	AfxBeginThread(ThreadProcRd,GetSafeHwnd());
}

void CMTSTestDlg::OnEndThread2() 
{
	// TODO: Add your control notification handler code here
	m_BeginTh2.EnableWindow(TRUE);
	m_EndTh2.EnableWindow(FALSE);
	g_Th2End=TRUE;
	EnableSelections();
	g_EndTh2.SetEvent();
}


void CMTSTestDlg::OnRadioCritical() 
{
	// TODO: Add your control notification handler code here
	if(m_Rd_Cs.GetCheck())
	{
		g_Sel=0;
	}
}

void CMTSTestDlg::OnRadioMutex() 
{
	// TODO: Add your control notification handler code here
	if(m_Rd_Mt.GetCheck())
	{
		g_Sel=1;
	}
}

void CMTSTestDlg::OnRadioSemaphores() 
{
	// TODO: Add your control notification handler code here
	if(m_Rd_Sp.GetCheck())
	{
		g_Sel=2;
	}
}

void CMTSTestDlg::EnableSelections(BOOL enb)
{
	if(enb==TRUE)
	{
		if(g_Th1End&&g_Th2End)
		{
			m_Rd_Cs.EnableWindow(TRUE);
			m_Rd_Mt.EnableWindow(TRUE);
			m_Rd_Sp.EnableWindow(TRUE);
		}
	}
	else
	{
		m_Rd_Cs.EnableWindow(FALSE);
		m_Rd_Mt.EnableWindow(FALSE);
		m_Rd_Sp.EnableWindow(FALSE);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -