读书人

进入多线程后程序乱跳不按照顺序执行

发布时间: 2012-03-25 20:55:16 作者: rapoo

进入多线程后程序乱跳,不按照顺序执行为什么?
// ServerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Server.h"
#include "ServerDlg.h"
#include "IpShow.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
char Tempbuffer[200];
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()

/////////////////////////////////////////////////////////////////////////////
// CServerDlg dialog

CServerDlg::CServerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CServerDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CServerDlg)
// 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);
}

void CServerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CServerDlg)
DDX_Control(pDX, IDC_LIST_USER, m_UserList);
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CServerDlg, CDialog)
//{{AFX_MSG_MAP(CServerDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_COMMAND(IDC_START, OnStart)
ON_COMMAND(IDC_STOP, OnStop)
ON_COMMAND(IDC_EXIT, OnExit)
ON_MESSAGE(MSG_ADDUSER, MSG_AddUser)
ON_MESSAGE(MSG_DELUSER, MSG_DelUser)
ON_COMMAND(IDC_INLINE, OnInline)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CServerDlg message handlers

BOOL CServerDlg::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

return TRUE; // return TRUE unless you set the focus to a control
}

void CServerDlg::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 CServerDlg::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 CServerDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}

BOOL CServerDlg::RecvData(CClientSocket *pSocket)
{
int nretval,nretvallen;
char buffer[120];
char recvbuffer[120];

//long lType, lLength;
memset(buffer, 0, 120);
memset(recvbuffer, 0, 120);


//nretval = pSocket->Receive(buffer, sizeof(buffer), 0);
//pSocket->Send("1",1,0);
//memcpy(m_strFileName,buffer,sizeof(buffer));
nretvallen = pSocket->Receive(recvbuffer, sizeof(recvbuffer), 0);
DWORD dwLength;
dwLength = *(DWORD*)(recvbuffer + sizeof(long));
m_dwFileLength = dwLength;

memset(Tempbuffer,0,200);
memcpy(Tempbuffer,"D:\\",4);
strcat(Tempbuffer,buffer);
//CFile file(Tempbuffer,CFile::modeCreate|CFile::modeWrite);
HANDLE hThread = CreateThread(NULL, 0U, RecvThreadProc, this, 0, NULL);
return TRUE;
}

DWORD WINAPI RecvThreadProc(LPVOID pParam)
{
CServerDlg *pDlg = (CServerDlg*)pParam;
int nMaxLength = pDlg->m_dwFileLength;
//int nMaxLength = 5040;
int nretval= 0, nrecvlen = 0;
int nProcLen = 0;

char bufferrecv[4096];
//FILE *fp = fopen(_T("D:\\FF.txt"), _T("wb+"));
while(nMaxLength > 0)
{
if(nMaxLength > 4096) nrecvlen = 4096;
else nrecvlen = nMaxLength;
if(!pDlg->RecvDataFile(bufferrecv, nrecvlen))
{
AfxMessageBox("接收失败!");
return -1;
}
nMaxLength -= nrecvlen;
//nProcLen += nrecvlen;
//fwrite(bufferrecv, 1, nrecvlen, fp);
//memset(bufferrecv,0,4096);
//pDlg->SendMessage(MSG_PROGRESS, nProcLen, 0);
}
//fclose(fp);
AfxMessageBox("接收完成!");
//closesocket(pDlg->pSocket);
return 0L;
}
BOOL CServerDlg::RecvDataFile(char *buffer, int len)
{
int nnMaxLength = len;
int nretvall, nrcvlenn = 0;
POSITION pos = m_UserInfoList.GetHeadPosition();
USERINFO *p = (USERINFO *)m_UserInfoList.GetNext(pos);
while(nnMaxLength > 0)
{
nretvall = (p->psock)->Receive((char*)(buffer+nrcvlenn), nnMaxLength, 0);
//nretvall=recv((unsigned int)(p->psock),(char*)(buffer+nrcvlenn), nnMaxLength, 0);
if(nretvall <=0)
{
//nretval = p->psock->Receive((char*)(buffer+nrcvlen), nnMaxLength, 0);
if(WSAGetLastError() != WSAEWOULDBLOCK)
{
return FALSE;
}
continue;
}
nrcvlenn += nretvall;
nnMaxLength -= nretvall;
}
return TRUE;
}

void CServerDlg::OnStart()
{
// TODO: Add your command handler code here
m_pAcceptSocket = new CAcceptSocket(this);
if(m_pAcceptSocket->Create(7999))
{
AfxMessageBox("成功!");
}
m_pAcceptSocket->AsyncSelect(FD_ACCEPT |FD_CLOSE);
m_pAcceptSocket->Listen();
}

void CServerDlg::OnStop()
{
// TODO: Add your command handler code here



m_pAcceptSocket->Close();
delete m_pAcceptSocket;
AfxMessageBox("服务器退出!");
}

void CServerDlg::OnExit()
{
// TODO: Add your command handler code here
OnStop();
SendMessage(WM_CLOSE, 0, 0);
}

LRESULT CServerDlg::MSG_AddUser(WPARAM wParam, LPARAM lParam)
{
USERINFO *pUserInfo = (USERINFO*)lParam;
m_UserInfoList.AddTail(pUserInfo);
CString strUserFlag;
strUserFlag.Format("%s:(%u)", inet_ntoa(*(in_addr*)&pUserInfo->addr),
pUserInfo->port);
m_UserList.AddString(strUserFlag);
return 0L;
}

LRESULT CServerDlg::MSG_DelUser(WPARAM wParam, LPARAM lParam)
{
CClientSocket *pSocket = (CClientSocket *)lParam;
POSITION pos = m_UserInfoList.GetHeadPosition();
POSITION tmp;
while(pos != NULL)
{
tmp = pos;
USERINFO *p = (USERINFO*)m_UserInfoList.GetNext(pos);
if(p->psock == pSocket)
{
CString strUserFlag;
strUserFlag.Format("%s:(%u)", inet_ntoa(*(in_addr*)&p->addr),
p->port);
int nIndex = m_UserList.FindString(0, strUserFlag);
if(nIndex != -1)
{
m_UserList.DeleteString(nIndex);
}
m_UserInfoList.RemoveAt(tmp);
break;
}
}
return 0L;
}

void CServerDlg::OnInline()
{
// TODO: Add your command handler code here
CIpShow Dlglook;
Dlglook.DoModal();
}
以上是全部代码,主要问题是,进入了BOOL CServerDlg::RecvDataFile(char *buffer, int len)这个函数后,执行nretvall = (p->psock)->Receive((char*)(buffer+nrcvlenn), nnMaxLength, 0);后程序有的时候正常,有的时候直接跳到BOOL CServerDlg::RecvData(CClientSocket *pSocket)这个函数里面,麻烦各位大侠帮忙看下,到底是哪儿出了问题,测试一天了,没有结果,谢谢了,本人比较菜,没有分,不好意思,麻烦大家帮忙了,本人QQ:15616100





[解决办法]
记住:即使是多线程/进程,程序也是顺序执行的
多线程/进程必然会跳,那是OS调度的原因。
VS2008/2010中打开“线程(Thread)”调试窗口,双击或右击一个线程,switch to thread就会跳到对应的线程上下文中

读书人网 >VC/MFC

热点推荐