多线程传参问题(急啊!)

blury 2007-09-07 10:44:32
typedef struct ThreadPara
{
CString commandCode;
CString blockId;
int length;
unsigned char* dataBuff;
}THREADPARA;

void CClientSocket::OnReceive(int nErrorCode)
{
CSocket::OnReceive(nErrorCode);
if (nErrorCode == 0)
{
unsigned char m_buffer[1024];
memset(m_buffer,0,sizeof(char)*1024);
int len = 0;
len = Receive(m_buffer,1024,0);
if (len <= 0) //if(len < 12)
return;
if (m_buffer[0]==0xFF && m_buffer[1]==0x0E)
{
CString strLength = "" ,command = "";
int length = 0;

strLength = HexToStr(8,10,m_buffer);
sscanf(strLength, "%x", &length);

command = HexToAscistr(10,11,m_buffer);

if (length == 0)
{
;
}
else
{
THREADPARA* threadParam = new THREADPARA;

threadParam->blockId.Format("%d",(int)m_buffer[7]);
threadParam->length = length;
threadParam->commandCode = command;
threadParam->dataBuff = m_buffer;

CString threadD = "";
threadD = HexToStr(0,len,threadParam->dataBuff);
AfxMessageBox("threadD:"+threadD); //已经测试数据正确
AfxBeginThread(newThreadProc,(LPVOID)threadParam); //creat a new thread
}

}

}

}

UINT CClientSocket::newThreadProc(LPVOID pParam)
{
BOOL terminalIs;
THREADPARA* threadParam = (THREADPARA*)pParam;

CString strstr = HexToStr(0,66,threadParam->dataBuff);
AfxMessageBox("strstr"+strstr); //有时候前32字节及倒数2字节正确,
//有时候就前5个字节正确,后面就不只是

//什么了,奇怪的是后面的错误数据有时候几次出现都是一样的,好像不是随机的,怎么回事?哪里

//出了问题?

AfxMessageBox(threadParam->blockId); //这里数据也正确
AfxMessageBox(threadParam->commandCode); //这里数据也正确

CString idTemp = HexToStr(2,6,threadParam->dataBuff);
AfxMessageBox("idid="+idTemp); //这就不对了,即使前32字节正确时,也不对


................................

delete threadParam;
return 0; //线程关了没? 需要void AfxEndThread(0); ???
}
CString CClientSocket::HexToStr(int i,int n, unsigned char* strPara) //类静态成员函数
{
char pTemp[2050];
char pDest[2050];
memset(pTemp,0,2050);
memset(pDest,0,2050);
for(int j = i;j <n; j++)
{
sprintf(pTemp,"%02X",strPara[j]);
strcat(pDest,pTemp);
}
return pDest;
}
高手帮忙看看,急啊!!!
...全文
217 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
blury 2007-09-13
  • 打赏
  • 举报
回复
结了,感谢 BUbuWander(Dead or alive,I'm coming with you.) 多次光顾!呵呵
BUbuWander 2007-09-10
  • 打赏
  • 举报
回复
tempDataBuff 先用 sscanf转换为 int 再使用 CString::Format()
codeangel 2007-09-10
  • 打赏
  • 举报
回复
mate
blury 2007-09-10
  • 打赏
  • 举报
回复
是参数生命期的问题吗?
blury 2007-09-09
  • 打赏
  • 举报
回复
放弃,重来。类似的问题出现:
SYTPICSvr.cpp :
unsigned char* tempDataBuff;


void CSYTPICSvrApp::AddMissionToThread(unsigned char *charData, CString commandCode,long length)
{
if (length<= 0)
return;

if ( length <= 1012)
{
length += 12;
}
else
{
length = 32;
}
if (length != 0)
{
tempDataBuff = charData;
CString testtest = HexToStr(0,length,tempDataBuff);
AfxMessageBox("lalala"+testtest); //显示数据完全正确
pMyThreadPoolManager->AddMission(&m_ThreadE);
//接下来运行ThreadE.cpp中的ExecThread()
}
}

ThreadE.cpp :

extern unsigned char* tempDataBuff;
void CThreadE::ExecThread()
{
long length = 0;
CString testData = ((CSYTPICSvrApp*)AfxGetApp())->HexToStr (0,1024,tempDataBuff);
AfxMessageBox("1212121212=="+testData );
//有时正确,有时不正确
CString blockId = "";
blockId.Format("%d",(int)tempDataBuff[7]);
AfxMessageBox("blockId=="+blockId);
//显示不正确,不管上面的正确与否,他都不正确。
CString strLength = ((CSYTPICSvrApp*)AfxGetApp())->HexToStr(8,10,tempDataBuff);
sscanf(strLength, "%x", &length);
AfxMessageBox("strLength=="+strLength);
//显示不正确,不管上面的正确与否,他都不正确。
}
tempDataBuff 到底是怎么回事啦?看懂得帮我看看哪里有问题?谢谢!
blury 2007-09-07
  • 打赏
  • 举报
回复
那块需要发消息阿?我不知道,我好像没用到。唉..什么也不懂,搞这玩艺儿,头疼死了。
BUbuWander 2007-09-07
  • 打赏
  • 举报
回复
sendmessage 之前没问题?
blury 2007-09-07
  • 打赏
  • 举报
回复
问题没变啊,每次第33字节为90,而倒数两字节却正确。
BUbuWander 2007-09-07
  • 打赏
  • 举报
回复
先 mark 再看,return 0;关了

你把return 0前代码用while(true)框起来再测试下,选一个情况break
blury 2007-09-07
  • 打赏
  • 举报
回复
谢谢 BUbuWander(Dead or alive,I'm coming with you.)

我看过你给的资料了。

可我只做了一个简单的工作线程,不用消息循环的。
BUbuWander 2007-09-07
  • 打赏
  • 举报
回复
http://www.vckbase.com/document/viewdoc/?id=1707
那先看看这个

2,586

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 资源
社区管理员
  • 资源
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧