我在做一个多线程同步问题的程序,程序没有出现错误,但是,动态截面不能运行,偶尔出现错误,提示Sleep(sleep)中的sleep没有初始化的信息,红色部分是出现过错误的地方!请大家帮忙!请加我QQ190231986 给我点建议!
下面是MoveDlg.cpp文件
#include "stdafx.h"
#include "Move.h"
#include "MoveDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define MAX_SPEED 50
#define MIN_SPEED 5
#define TIME_OUT 100
#define RED 0
#define GREEN 1
#define BLUE 2
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
DWORD WINAPI BallMove(LPVOID lpparameter)
{
LPINFO temp=(LPINFO)lpparameter;
BOOL xadd,yadd;
HANDLE hMutex;
long speed;
//取得相应的小求的互斥量的句柄
if(temp->color==RED)
hMutex=OpenMutex(MUTEX_ALL_ACCESS,FALSE,"RedSpeed");
else if(temp->color==GREEN)
hMutex=OpenMutex(MUTEX_ALL_ACCESS,FALSE,"GreenSpeed");
else if(temp->color==BLUE)
hMutex=OpenMutex(MUTEX_ALL_ACCESS,FALSE,"BlueSpeed");
if(hMutex==NULL)
return 1;
xadd=yadd=TRUE;
while(TRUE)
{
if(WaitForSingleObject(hMutex,TIME_OUT)==WAIT_FAILED)
continue;
if((temp->xPos+temp->ballr)>=((LPRECT)temp->rect)->right)
xadd=FALSE;
else if((temp->xPos-temp->ballr)<=((LPRECT)temp->rect)->left)
xadd=TRUE;
if((temp->yPos+temp->ballr)>=((LPRECT)temp->rect)->bottom)
yadd=FALSE;
else if((temp->yPos-temp->ballr)<=((LPRECT)temp->rect)->top)
yadd=TRUE;
if(xadd)
temp->xPos++;
else temp->xPos--;
if(yadd)
temp->yPos++;
else temp->yPos--;
CRect rect(temp->xPos-temp->ballr-2,temp->yPos-temp->ballr-2,temp->xPos+temp->ballr+2,temp->yPos+temp->ballr+2);
InvalidateRect(temp->hWnd,rect,TRUE);
Sleep(temp->speed);
ReleaseMutex(hMutex);
Sleep(speed);
}
return 0;
}
HANDLE h_gRMutex;
HANDLE h_gGMutex;
HANDLE h_gBMutex;
class CAboutDlg : public CDialog
{
public:
CMoveDlg Create;
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()
/////////////////////////////////////////////////////////////////////////////
// CMoveDlg dialog
CMoveDlg::CMoveDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMoveDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMoveDlg)
// 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);
m_IsGreen=m_IsBlue=m_IsRed=FALSE;
h_gRMutex=CreateMutex(NULL,FALSE,"RedSpeed");
h_gGMutex=CreateMutex(NULL,FALSE,"GreenSpeed");
h_gBMutex=CreateMutex(NULL,FALSE,"BlueSpeed");
if((h_gRMutex==NULL)||(h_gGMutex==NULL)||(h_gBMutex==NULL))
{
AfxMessageBox("生成互斥量失败");
CloseHandle(h_gRMutex);
CloseHandle(h_gGMutex);
CloseHandle(h_gBMutex);
OnOK();
}
}
void CMoveDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMoveDlg)
DDX_Control(pDX, IDC_SPEED_RED, m_RSpeed);
DDX_Control(pDX, IDC_SPEED_GREEN, m_GSpeed);
DDX_Control(pDX, IDC_SPEED_BLUE, m_BSpeed);
DDX_Control(pDX, IDC_MOVEBOX, m_MoveBox);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMoveDlg, CDialog)
//{{AFX_MSG_MAP(CMoveDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_END, OnButtonEnd)
ON_BN_CLICKED(IDC_BUTTON_START, OnButtonStart)
ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SPEED_BLUE, OnReleasedcaptureSpeedBlue)
ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SPEED_GREEN, OnReleasedcaptureSpeedGreen)
ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SPEED_RED, OnReleasedcaptureSpeedRed)
ON_WM_CLOSE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMoveDlg message handlers
BOOL CMoveDlg::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
m_RSpeed.SetRangeMax(MAX_SPEED);
m_RSpeed.SetRangeMin(MIN_SPEED);
m_GSpeed.SetRangeMax(MAX_SPEED);
m_GSpeed.SetRangeMin(MIN_SPEED);
m_BSpeed.SetRangeMax(MAX_SPEED);
m_BSpeed.SetRangeMin(MIN_SPEED);
GetDlgItem(IDC_BUTTON_START)->EnableWindow(TRUE);
GetDlgItem(IDC_BUTTON_END)->EnableWindow(FALSE);
GetDlgItem(IDC_SPEED_RED)->EnableWindow(FALSE);
GetDlgItem(IDC_SPEED_GREEN)->EnableWindow(FALSE);
GetDlgItem(IDC_SPEED_BLUE)->EnableWindow(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
void CMoveDlg::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 CMoveDlg::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();
}
if(m_IsGreen)
{
CDC* pDC=m_MoveBox.GetDC();
CBrush * poldbrush;
CBrush brush(RGB(0,255,0));
poldbrush=pDC->SelectObject(&brush);
pDC->Ellipse(m_GreenBall.xPos-((LPRECT)m_GreenBall.rect)->left-m_GreenBall.ballr,m_GreenBall.yPos-((LPRECT)m_GreenBall.rect)->top-m_GreenBall.ballr,m_GreenBall.xPos-((LPRECT)m_GreenBall.rect)->left+m_GreenBall.ballr,m_GreenBall.yPos-((LPRECT)m_GreenBall.rect)->top+m_GreenBall.ballr);
pDC->SelectObject(poldbrush);
}
if(m_IsBlue)
{
CDC* pDC=m_MoveBox.GetDC();
CBrush * poldbrush;
CBrush brush(RGB(0,0,255));
poldbrush=pDC->SelectObject(&brush);
pDC->Ellipse(m_BlueBall.xPos-((LPRECT)m_BlueBall.rect)->left-m_BlueBall.ballr,m_BlueBall.yPos-((LPRECT)m_BlueBall.rect)->top-m_BlueBall.ballr,m_BlueBall.xPos-((LPRECT)m_BlueBall.rect)->left+m_BlueBall.ballr,m_BlueBall.yPos-((LPRECT)m_BlueBall.rect)->top+m_BlueBall.ballr);
pDC->SelectObject(poldbrush);
}
if(m_IsRed)
{
CDC* pDC=m_MoveBox.GetDC();
CBrush * poldbrush;
CBrush brush(RGB(255,0,0));
poldbrush=pDC->SelectObject(&brush);
pDC->Ellipse(m_RedBall.xPos-((LPRECT)m_RedBall.rect)->left-m_RedBall.ballr,m_RedBall.yPos-((LPRECT)m_RedBall.rect)->top-m_RedBall.ballr,m_RedBall.xPos-((LPRECT)m_RedBall.rect)->left+m_RedBall.ballr,m_RedBall.yPos-((LPRECT)m_RedBall.rect)->top+m_RedBall.ballr);
pDC->SelectObject(poldbrush);
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CMoveDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMoveDlg::OnButtonStart()
{
// TODO: Add your control notification handler code here
DWORD ThreadID;
DWORD code;
CRect rect;
GetClientRect(rect);
ClientToScreen(rect);
m_MoveBox.GetClientRect(m_MoveRect);
m_MoveBox.ClientToScreen(m_MoveRect);
m_MoveRect-=rect.TopLeft();
if(!m_IsGreen)
{
m_GreenBall.hWnd=m_hWnd;
m_GreenBall.ballr=8;
m_GreenBall.speed=10;
m_GreenBall.xPos=((LPRECT)m_MoveRect)->left+m_GreenBall.ballr;
m_GreenBall.yPos=((LPRECT)m_MoveRect)->top+m_GreenBall.ballr;
m_GreenBall.rect=m_MoveRect;
m_IsGreen=TRUE;
}
if(!GetExitCodeThread(m_hGreenThread,&code)||(code!=STILL_ACTIVE))
m_hGreenThread=CreateThread(NULL,0,BallMove,&m_GreenBall,0,&ThreadID);
if(m_hGreenThread==NULL)
{
MessageBox("Create thread Green Ball failed!");
m_IsGreen=FALSE;
}
if(!m_IsBlue)
{
m_BlueBall.hWnd=m_hWnd;
m_BlueBall.ballr=9;
m_BlueBall.speed=20;
m_BlueBall.xPos=((LPRECT)m_MoveRect)->left+m_BlueBall.ballr+50;
m_BlueBall.yPos=((LPRECT)m_MoveRect)->top+m_BlueBall.ballr+60;
m_BlueBall.rect=m_MoveRect;
m_IsBlue=TRUE;
}
if(!GetExitCodeThread(m_hBlueThread,&code)||(code!=STILL_ACTIVE))
m_hBlueThread=CreateThread(NULL,0,BallMove,&m_BlueBall,0,&ThreadID);
if(m_hBlueThread==NULL)
{
MessageBox("Create thread Blue Ball failed!");
m_IsBlue=FALSE;
}
if(!m_IsRed)
{
m_RedBall.hWnd=m_hWnd;
m_RedBall.ballr=7;
m_RedBall.speed=15;
m_RedBall.xPos=((LPRECT)m_MoveRect)->left+m_RedBall.ballr+100;
m_RedBall.yPos=((LPRECT)m_MoveRect)->top+m_RedBall.ballr+30;
m_RedBall.rect=m_MoveRect;
m_IsRed=TRUE;
}
if(!GetExitCodeThread(m_hRedThread,&code)||(code!=STILL_ACTIVE))
m_hRedThread=CreateThread(NULL,0,BallMove,&m_RedBall,0,&ThreadID);
if(m_hRedThread==NULL)
{
MessageBox("Create thread Red Ball failed!");
m_IsRed=FALSE;
}
GetDlgItem(IDC_BUTTON_START)->EnableWindow(FALSE);
GetDlgItem(IDC_BUTTON_END)->EnableWindow(TRUE);
GetDlgItem(IDC_SPEED_RED)->EnableWindow(TRUE);
GetDlgItem(IDC_SPEED_GREEN)->EnableWindow(TRUE);
GetDlgItem(IDC_SPEED_BLUE)->EnableWindow(TRUE);
}
void CMoveDlg::OnButtonEnd()
{
// TODO: Add your control notification handler code here
DWORD code;
if(GetExitCodeThread(m_hGreenThread,&code))
if(code==STILL_ACTIVE)
{
TerminateThread(m_hGreenThread,0);
CloseHandle(m_hGreenThread);
}
if(GetExitCodeThread(m_hBlueThread,&code))
if(code==STILL_ACTIVE)
{
TerminateThread(m_hBlueThread,0);
CloseHandle(m_hBlueThread);
}
if(GetExitCodeThread(m_hRedThread,&code))
if(code==STILL_ACTIVE)
{
TerminateThread(m_hRedThread,0);
CloseHandle(m_hRedThread);
}
GetDlgItem(IDC_BUTTON_START)->EnableWindow(TRUE);
GetDlgItem(IDC_BUTTON_END)->EnableWindow(FALSE);
GetDlgItem(IDC_SPEED_RED)->EnableWindow(FALSE);
GetDlgItem(IDC_SPEED_GREEN)->EnableWindow(FALSE);
GetDlgItem(IDC_SPEED_BLUE)->EnableWindow(FALSE);
}
void CMoveDlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
OnButtonEnd();
CloseHandle(h_gRMutex);
CloseHandle(h_gGMutex);
CloseHandle(h_gBMutex);
CDialog::OnClose();
}
BOOL CMoveDlg::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
{
// TODO: Add your specialized code here and/or call the base class
return CDialog::Create(IDD, pParentWnd);
}
void CMoveDlg::OnReleasedcaptureSpeedBlue(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
if(WaitForSingleObject(h_gBMutex,TIME_OUT)!=WAIT_FAILED)
{
m_BlueBall.speed=MAX_SPEED-m_BSpeed.GetPos();
ReleaseMutex(h_gBMutex);
}
*pResult = 0;
}
void CMoveDlg::OnReleasedcaptureSpeedGreen(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
if(WaitForSingleObject(h_gGMutex,TIME_OUT)!=WAIT_FAILED)
{
m_GreenBall.speed=MAX_SPEED-m_GSpeed.GetPos();
ReleaseMutex(h_gGMutex);
}
*pResult = 0;
}
void CMoveDlg::OnReleasedcaptureSpeedRed(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
if(WaitForSingleObject(h_gRMutex,TIME_OUT)!=WAIT_FAILED)
{
m_RedBall.speed=MAX_SPEED-m_RSpeed.GetPos();
ReleaseMutex(h_gRMutex);
}
*pResult = 0;
}