| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3953 人关注过本帖
标题:【求助】创建新线程后编译出错
只看楼主 加入收藏
Scofield
Rank: 1
等 级:新手上路
威 望:1
帖 子:56
专家分:0
注 册:2006-5-30
收藏
 问题点数:0 回复次数:3 
【求助】创建新线程后编译出错

是一个数据传输程序,开始用的是单线程,但是当程序运行后用户就不能对界面的按钮操作了,当然数据还是继续传输的 :(

于是就想弄个新线程专门用来传输数据(应该可行吧?)

于是在NewDlg.h中新加一线程函数 static UINT ReceiveThread(void *param);

在NewDlg.app需要调用线程的函数里面添加了 AfxBeginThread(ReceiveThread,this);

然后线程ReceiveThread是这么定义的:

#include <iostream>
#include <fstream>
using std::ofstream;
using std::ifstream;
using std::ios;
using std::endl;

void CNewDlg::OnButtonSenddata()
{
// TODO: Add your control notification handler code here
if(m_connect==0)
{

ShowInfo("设备未连接",2); ////////////////信息框输出
return;
}

AfxBeginThread(ReceiveThread,this);

}


int decimal, sign;
UINT CNewDlg::ReceiveThread(void *param)
{
CNewDlg *dlg = (CNewDlg*)param;

if(dlg==NULL)
return -1;

ifstream InFile("test1.txt",ios::in);
ofstream OutFile("备份说明文档1.txt",ios::out);

if (!InFile)

{

MessageBox("出错!请检查您的源数据文件是否存在!","警告",MB_ICONQUESTION);
return -1; }
if( !OutFile )
{
MessageBox("创建文件出错!");
return -1;
}

double FileData[1];
CString str_double;

int datasum=0; //数据个数
double atime=0; //传输所需平均时间

while(InFile >> FileData[0])
{
LARGE_INTEGER litmp; //采用QueryPerformanceFrequency和QueryPerformanceCounter延迟20ms
LONGLONG QPart1,QPart2;

double dfMinus,dfFreq,dfTim;

QueryPerformanceFrequency(&litmp);
dfFreq = (double)litmp.QuadPart;

QueryPerformanceCounter(&litmp);
QPart1 = litmp.QuadPart;

for(;;)
{
QueryPerformanceCounter(&litmp);
QPart2 = litmp.QuadPart;

dfMinus = (double)(QPart2-QPart1);
dfTim = dfMinus/dfFreq;

if(dfTim >= 0.02)
break;
}
str_double = _fcvt (FileData[0], 8, &decimal, &sign);

m_EditSendData=str_double;

VCI_CAN_OBJ frameinfo;

char szFrameID[9];
unsigned char FrameID[4]={0,0,0,0};
memset(szFrameID,'0',9);
char szData[9];
BYTE datalen=8;

UpdateData(true);

memcpy(&szFrameID[8-m_EditSendFrmID.GetLength()],(LPCTSTR)m_EditSendFrmID,m_EditSendFrmID.GetLength());
strtodata((unsigned char*)szFrameID,FrameID,4,0);

strcpy(szData,(LPCTSTR)m_EditSendData);

UpdateData(false);
frameinfo.DataLen=datalen;
memcpy(&frameinfo.Data,m_EditSendData,strlen(m_EditSendData));

int b=strlen(m_EditSendData);

for(int i=strlen(m_EditSendData);i<8;i++)
{
frameinfo.Data[i]='\0';
}

frameinfo.RemoteFlag=m_ComboSendFrmFmt;

frameinfo.ExternFlag=m_ComboSendFrmType;
frameinfo.ID=((DWORD)FrameID[0]<<24)+((DWORD)FrameID[1]<<16)+((DWORD)FrameID[2]<<8)+
((DWORD)FrameID[3]);

frameinfo.SendType=0;

QueryPerformanceCounter(&litmp);
QPart1 = litmp.QuadPart;

if(VCI_Transmit(m_devtype,m_devind,m_cannum0,&frameinfo,1)==1) //发送数据
{

ShowInfo("通道0发送成功",0);

}

if(VCI_Receive(m_devtype,m_devind,m_cannum1,&frameinfo,1,100)==1) //接受数据
{
QueryPerformanceCounter(&litmp);
QPart2 = litmp.QuadPart;

dfMinus = (double)(QPart2-QPart1);
dfTim = dfMinus/dfFreq;
atime+=dfTim;
datasum++;

CString str,tmpstr,strdfTim;
double Receive_Double[1],Double_Data;
char *restr_double=(char *)Receive_Double;
CString restr_double2;

for(int i=0;i<8;i++)
{
restr_double[i]=frameinfo.Data[i]; //BYTE__>char
}
restr_double2=restr_double; //char__>CString
Receive_Double[0]=atof((LPCTSTR)restr_double2); //CString__>double

Double_Data=Receive_Double[0]/pow(10,8-decimal);

if(sign==1 &&decimal < 0)
{
Double_Data = -Receive_Double[0]/pow(10,8);
}
if(sign==1 && decimal >= 0)
{
Double_Data = -Receive_Double[0]/pow(10,8-decimal);
}
if(sign==0 &&decimal < 0)
{
Double_Data = Receive_Double[0]/pow(10,8);
}

strdfTim.Format("%f",dfTim);
str="通道1接收成功";
tmpstr=" 数据: ";
str+=tmpstr;

tmpstr.Format("%12.8f",Double_Data);
str+=tmpstr;
tmpstr=" 传输数据所用时间: ";
str+=tmpstr;
tmpstr=strdfTim;
str+=tmpstr;
tmpstr=" 秒 ";
str+=tmpstr;

char *p;
p=(char *)(LPCTSTR)str;

m_ListInfo.InsertString(m_ListInfo.GetCount(),str);
m_ListInfo.SetCurSel(m_ListInfo.GetCount()-1);
OutFile << p <<endl;

}
else
{
ShowInfo("发送失败",2);
return -1;
}
}

CString str,tmpstr;
char *p;

str="数据发送完成!";
tmpstr=" 数据总个数: ";
str+=tmpstr;
tmpstr.Format("%d",datasum);
str+=tmpstr;
tmpstr=" 个, 数据平均传输时间: ";
str+=tmpstr;
tmpstr.Format("%f",atime/datasum);
str+=tmpstr;
tmpstr=" 秒";
str+=tmpstr;
p=(char *)(LPCTSTR)str;

m_ListInfo.InsertString(m_ListInfo.GetCount(),str);
m_ListInfo.SetCurSel(m_ListInfo.GetCount()-1);
OutFile << p <<endl;

return 0;
}

程序编译时出现了如下的错误:

Compiling...
NewDlg.cpp
E:\程序学习\VC++\MFC\New2\NewDlg.cpp(604) : error C2352: 'CWnd::MessageBoxA' : illegal call of non-static member function
c:\program files\microsoft visual studio\vc98\mfc\include\afxwin.h(2197) : see declaration of 'MessageBoxA'
E:\程序学习\VC++\MFC\New2\NewDlg.cpp(610) : error C2352: 'CWnd::MessageBoxA' : illegal call of non-static member function
c:\program files\microsoft visual studio\vc98\mfc\include\afxwin.h(2197) : see declaration of 'MessageBoxA'
E:\程序学习\VC++\MFC\New2\NewDlg.cpp(645) : error C2597: illegal reference to data member 'CNewDlg::m_EditSendData' in a static member function
E:\程序学习\VC++\MFC\New2\NewDlg.cpp(654) : error C2352: 'CWnd::UpdateData' : illegal call of non-static member function
c:\program files\microsoft visual studio\vc98\mfc\include\afxwin.h(2230) : see declaration of 'UpdateData'
E:\程序学习\VC++\MFC\New2\NewDlg.cpp(656) : error C2228: left of '.GetLength' must have class/struct/union type
E:\程序学习\VC++\MFC\New2\NewDlg.cpp(656) : error C2440: 'type cast' : cannot convert from 'class CString CNewDlg::*' to 'const char *'
There is no context in which this conversion is possible
E:\程序学习\VC++\MFC\New2\NewDlg.cpp(656) : error C2228: left of '.GetLength' must have class/struct/union type
E:\程序学习\VC++\MFC\New2\NewDlg.cpp(657) : error C2352: 'CNewDlg::strtodata' : illegal call of non-static member function
e:\程序学习\vc++\mfc\new2\newdlg.h(41) : see declaration of 'strtodata'
E:\程序学习\VC++\MFC\New2\NewDlg.cpp(658) : error C2440: 'type cast' : cannot convert from 'class CString CNewDlg::*' to 'const char *'
There is no context in which this conversion is possible
E:\程序学习\VC++\MFC\New2\NewDlg.cpp(660) : error C2352: 'CWnd::UpdateData' : illegal call of non-static member function
c:\program files\microsoft visual studio\vc98\mfc\include\afxwin.h(2230) : see declaration of 'UpdateData'
E:\程序学习\VC++\MFC\New2\NewDlg.cpp(663) : error C2597: illegal reference to data member 'CNewDlg::m_EditSendData' in a static member function
E:\程序学习\VC++\MFC\New2\NewDlg.cpp(663) : error C2597: illegal reference to data member 'CNewDlg::m_EditSendData' in a static member function
E:\程序学习\VC++\MFC\New2\NewDlg.cpp(665) : error C2597: illegal reference to data member 'CNewDlg::m_EditSendData' in a static member function
E:\程序学习\VC++\MFC\New2\NewDlg.cpp(667) : error C2597: illegal reference to data member 'CNewDlg::m_EditSendData' in a static member function
E:\程序学习\VC++\MFC\New2\NewDlg.cpp(672) : error C2597: illegal reference to data member 'CNewDlg::m_ComboSendFrmFmt' in a static member function
E:\程序学习\VC++\MFC\New2\NewDlg.cpp(673) : error C2597: illegal reference to data member 'CNewDlg::m_ComboSendFrmType' in a static member function
E:\程序学习\VC++\MFC\New2\NewDlg.cpp(682) : error C2597: illegal reference to data member 'CNewDlg::m_devtype' in a static member function
E:\程序学习\VC++\MFC\New2\NewDlg.cpp(682) : error C2597: illegal reference to data member 'CNewDlg::m_devind' in a static member function
E:\程序学习\VC++\MFC\New2\NewDlg.cpp(682) : error C2597: illegal reference to data member 'CNewDlg::m_cannum0' in a static member function
E:\程序学习\VC++\MFC\New2\NewDlg.cpp(685) : error C2352: 'CNewDlg::ShowInfo' : illegal call of non-static member function
e:\程序学习\vc++\mfc\new2\newdlg.h(30) : see declaration of 'ShowInfo'
E:\程序学习\VC++\MFC\New2\NewDlg.cpp(689) : error C2597: illegal reference to data member 'CNewDlg::m_devtype' in a static member function
E:\程序学习\VC++\MFC\New2\NewDlg.cpp(689) : error C2597: illegal reference to data member 'CNewDlg::m_devind' in a static member function
E:\程序学习\VC++\MFC\New2\NewDlg.cpp(689) : error C2597: illegal reference to data member 'CNewDlg::m_cannum1' in a static member function
E:\程序学习\VC++\MFC\New2\NewDlg.cpp(743) : error C2228: left of '.InsertString' must have class/struct/union type
E:\程序学习\VC++\MFC\New2\NewDlg.cpp(743) : error C2228: left of '.GetCount' must have class/struct/union type
E:\程序学习\VC++\MFC\New2\NewDlg.cpp(744) : error C2228: left of '.SetCurSel' must have class/struct/union type
E:\程序学习\VC++\MFC\New2\NewDlg.cpp(744) : error C2228: left of '.GetCount' must have class/struct/union type
E:\程序学习\VC++\MFC\New2\NewDlg.cpp(750) : error C2352: 'CNewDlg::ShowInfo' : illegal call of non-static member function
e:\程序学习\vc++\mfc\new2\newdlg.h(30) : see declaration of 'ShowInfo'
E:\程序学习\VC++\MFC\New2\NewDlg.cpp(771) : error C2228: left of '.InsertString' must have class/struct/union type
E:\程序学习\VC++\MFC\New2\NewDlg.cpp(771) : error C2228: left of '.GetCount' must have class/struct/union type
E:\程序学习\VC++\MFC\New2\NewDlg.cpp(772) : error C2228: left of '.SetCurSel' must have class/struct/union type
E:\程序学习\VC++\MFC\New2\NewDlg.cpp(772) : error C2228: left of '.GetCount' must have class/struct/union type
Error executing cl.exe.

NewDlg.obj - 32 error(s), 0 warning(s)


是不是新建的线程对调用外部变量或函数有什么要求呢?不知道该怎么修改啊?希望大侠们能帮帮忙啊

小弟先谢过了!

[此贴子已经被作者于2006-8-9 11:07:47编辑过]

搜索更多相关主题的帖子: 线程 数据传输 std using 编译 
2006-08-09 10:17
Scofield
Rank: 1
等 级:新手上路
威 望:1
帖 子:56
专家分:0
注 册:2006-5-30
收藏
得分:0 

嗯,现在在语句

UINT CNewDlg::ReceiveThread(void *param)
{
CNewDlg *dlg = (CNewDlg*)param; 后添加了一条

dlg->GetParent();
然后在所有的在CNewDlg中定义的变量前均添加了“dlg->”,

编译后系统只提出了2条错误:

Compiling...
NewDlg.cpp
E:\程序学习\VC++\MFC\New2\NewDlg.cpp(614) : error C2352: 'CWnd::MessageBoxA' : illegal call of non-static member function
c:\program files\microsoft visual studio\vc98\mfc\include\afxwin.h(2197) : see declaration of 'MessageBoxA'
E:\程序学习\VC++\MFC\New2\NewDlg.cpp(620) : error C2352: 'CWnd::MessageBoxA' : illegal call of non-static member function
c:\program files\microsoft visual studio\vc98\mfc\include\afxwin.h(2197) : see declaration of 'MessageBoxA'
Error executing cl.exe.

NewDlg.obj - 2 error(s), 0 warning(s)

请问这个系统函数MessageBox该怎么引用呢?


2006-08-09 16:34
yeshirow
Rank: 4
等 级:贵宾
威 望:10
帖 子:854
专家分:0
注 册:2006-6-8
收藏
得分:0 

CWnd 類的 MessageBoxA 不是 CWnd 的靜態成員, 當然不能用 CWnd:: 來訪問了,

不過可以使用全局的 MessageBox

::MessageBox(arglist....);


原來朋友仔感情再天真, 亦是我永遠也會愛惜的人, 明日愛他人, 也記住學會不要緊; 原來朋友比戀人更高分, 亦讓我開始懂得不記恨, 若大家都敏感, 我更要永遠記得拒絕再因小事怪人, 爲何沒有這條校訓...Twins-朋友仔 MCSD Training
2006-08-10 00:48
Scofield
Rank: 1
等 级:新手上路
威 望:1
帖 子:56
专家分:0
注 册:2006-5-30
收藏
得分:0 

我用API函数AfxMessageBox了 :)

谢谢楼上的


2006-08-12 22:01
快速回复:【求助】创建新线程后编译出错
数据加载中...
 
   



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

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