| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1980 人关注过本帖
标题:[求助]VC6.0多线程同步问题截面不能运行
只看楼主 加入收藏
heshuying
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2006-5-16
收藏
 问题点数:0 回复次数:4 
[求助]VC6.0多线程同步问题截面不能运行

我在做一个多线程同步问题的程序,程序没有出现错误,但是,动态截面不能运行,偶尔出现错误,提示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;
}

搜索更多相关主题的帖子: 截面 线程 include 
2006-05-17 18:35
heshuying
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2006-5-16
收藏
得分:0 
沙发!请各位帮忙啊!觉得程序不够的话可以回复啊!我尽量满足啊!
2006-05-17 18:36
myajax95
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:30
帖 子:2978
专家分:0
注 册:2006-3-5
收藏
得分:0 
太长了,没全看完,楼主想实现什么功能?就是定时让步同颜色的控件移动一下吗?

http://myajax95./
2006-05-23 04:11
heshuying
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2006-5-16
收藏
得分:0 
[求助]

我把MoveDlg.h文件也补充上来了啊!就是不能运行啊!模拟系统不能动啊!不知道为什么啊

/ MoveDlg.h : header file
//

#if !defined(AFX_MOVEDLG_H__00870F66_5402_400F_B89E_D930B2C12590__INCLUDED_)
#define AFX_MOVEDLG_H__00870F66_5402_400F_B89E_D930B2C12590__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

/////////////////////////////////////////////////////////////////////////////
// CMoveDlg dialog
typedef struct info{
HWND hWnd;
long speed;
long xPos;
long yPos;
long ballr;
DWORD color;
CRect rect;
}INFO,*LPINFO;

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

// Dialog Data
//{{AFX_DATA(CMoveDlg)
enum { IDD = IDD_MOVE_DIALOG };
CSliderCtrl m_RSpeed;
CSliderCtrl m_GSpeed;
CSliderCtrl m_BSpeed;
CStatic m_MoveBox;
//}}AFX_DATA

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMoveDlg)
public:
virtual BOOL Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext = NULL);
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL

// Implementation
protected:
HICON m_hIcon;
CRect m_MoveRect;
INFO m_GreenBall;
INFO m_RedBall;
INFO m_BlueBall;
HANDLE m_hGreenThread;
HANDLE m_hBlueThread;
HANDLE m_hRedThread;
BOOL m_IsGreen;
BOOL m_IsBlue;
BOOL m_IsRed;


// Generated message map functions
//{{AFX_MSG(CMoveDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnButtonEnd();
afx_msg void OnButtonStart();
afx_msg void OnReleasedcaptureSpeedBlue(NMHDR* pNMHDR, LRESULT* pResult);
afx_msg void OnReleasedcaptureSpeedGreen(NMHDR* pNMHDR, LRESULT* pResult);
afx_msg void OnReleasedcaptureSpeedRed(NMHDR* pNMHDR, LRESULT* pResult);
afx_msg void OnClose();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

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

#endif // !defined(AFX_MOVEDLG_H__00870F66_5402_400F_B89E_D930B2C12590__INCLUDED_)

2006-05-26 14:55
heshuying
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2006-5-16
收藏
得分:0 
[讨论]
我的目的就是让一个多线程的模拟系统运行,但是,程序没有出现什么错误,就是不动.大家看看我红色的字的地方,看看是不是有错误了!反正大家有某些建议就提出来啊!我都尝试!谢谢啦!!
2006-05-26 14:59
快速回复:[求助]VC6.0多线程同步问题截面不能运行
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.024935 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved