是一个数据传输程序,开始用的是单线程,但是当程序运行后用户就不能对界面的按钮操作了,当然数据还是继续传输的 :(
于是就想弄个新线程专门用来传输数据(应该可行吧?)
于是在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编辑过]