我在程序里是这样做的:
// CMPP120Dlg.cpp : implementation file
//#include "stdafx.h"
#include "CMPP120.h"
#include "CMPP120Dlg.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//----------------------------------------
UINT WorkerThread( LPVOID pParam )
{
//接收一个窗口类指针,然后设置窗口标题
AfxMessageBox("OK");
CWnd *pstaTimer=(CWnd*)pParam;
for(int i=0;i<100;i++)
{
//TRACE("thread %d\n",i);
char szT[100];
sprintf(szT,"worker thread : %d",i);
pstaTimer->SetWindowText(szT);
Sleep(10);
}
return 0; // 返回并退出线程
//或者调用void AfxEndThread( UINT nExitCode );来退出
}
//----------------------------------------
UINT SocketThread( LPVOID pParam )
{
//接收一个窗口类指针,然后设置窗口标题
CCMPP120Dlg *mDlg;//=(CCMPP120Dlg*)pParam;
CSocket* mSock=(CSocket*)pParam;
char mBuff[4096];
int len;
while(!(mDlg->mText=="exit"))
{
len=mSock->Receive(mBuff,4096,0) ;
if (len>0)
{
mBuff[len]='\0';
mDlg->mRecv=mBuff;
mDlg->UpdateData(FALSE);
}
Sleep(100);
} /*for(int i=0;i<100;i++)
{
//TRACE("thread %d\n",i);
char szT[100];
sprintf(szT,"worker thread : %d",i);
pstaTimer->SetWindowText(szT);
Sleep(10);
}*/
return 0; // 返回并退出线程
//或者调用void AfxEndThread( UINT nExitCode );来退出
}
//-------------------------------------
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App Aboutclass 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()/////////////////////////////////////////////////////////////////////////////
// CCMPP120Dlg dialogCCMPP120Dlg::CCMPP120Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CCMPP120Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCMPP120Dlg)
mRecv = _T("");
mText = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}void CCMPP120Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCMPP120Dlg)
DDX_Control(pDX, IDC_STATIC1, m_REC);
DDX_Text(pDX, IDC_EDIT1, mRecv);
DDX_Text(pDX, IDC_EDIT2, mText);
//}}AFX_DATA_MAP
}BEGIN_MESSAGE_MAP(CCMPP120Dlg, CDialog)
//{{AFX_MSG_MAP(CCMPP120Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CCMPP120Dlg message handlersBOOL CCMPP120Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
//CCMPP120Dlg* Dlg;
//Dlg->OnInitDialog;
mHost="127.0.0.1";
mPort=8801;
if(!mSocket.Create())
{
AfxMessageBox("套接字建立错误!");
return TRUE;
}
if(!mSocket.Connect(mHost,mPort))
{
AfxMessageBox("连接不到服务器!");
return TRUE;
}
////////////////////////////////////////////////////////////
在这里能传入窗体吗?
AfxBeginThread(SocketThread,&mSocket);
///////////////////////////////////////////////////////////////
//UpdateData(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CCMPP120Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}void CCMPP120Dlg::OnButton1()
{
UpdateData();
mSocket.Send(mText,mText.GetLength());
}
// CMPP120Dlg.cpp : implementation file
//#include "stdafx.h"
#include "CMPP120.h"
#include "CMPP120Dlg.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//----------------------------------------
UINT WorkerThread( LPVOID pParam )
{
//接收一个窗口类指针,然后设置窗口标题
AfxMessageBox("OK");
CWnd *pstaTimer=(CWnd*)pParam;
for(int i=0;i<100;i++)
{
//TRACE("thread %d\n",i);
char szT[100];
sprintf(szT,"worker thread : %d",i);
pstaTimer->SetWindowText(szT);
Sleep(10);
}
return 0; // 返回并退出线程
//或者调用void AfxEndThread( UINT nExitCode );来退出
}
//----------------------------------------
UINT SocketThread( LPVOID pParam )
{
//接收一个窗口类指针,然后设置窗口标题
CCMPP120Dlg *mDlg;//=(CCMPP120Dlg*)pParam;
CSocket* mSock=(CSocket*)pParam;
char mBuff[4096];
int len;
while(!(mDlg->mText=="exit"))
{
len=mSock->Receive(mBuff,4096,0) ;
if (len>0)
{
mBuff[len]='\0';
mDlg->mRecv=mBuff;
mDlg->UpdateData(FALSE);
}
Sleep(100);
} /*for(int i=0;i<100;i++)
{
//TRACE("thread %d\n",i);
char szT[100];
sprintf(szT,"worker thread : %d",i);
pstaTimer->SetWindowText(szT);
Sleep(10);
}*/
return 0; // 返回并退出线程
//或者调用void AfxEndThread( UINT nExitCode );来退出
}
//-------------------------------------
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App Aboutclass 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()/////////////////////////////////////////////////////////////////////////////
// CCMPP120Dlg dialogCCMPP120Dlg::CCMPP120Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CCMPP120Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCMPP120Dlg)
mRecv = _T("");
mText = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}void CCMPP120Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCMPP120Dlg)
DDX_Control(pDX, IDC_STATIC1, m_REC);
DDX_Text(pDX, IDC_EDIT1, mRecv);
DDX_Text(pDX, IDC_EDIT2, mText);
//}}AFX_DATA_MAP
}BEGIN_MESSAGE_MAP(CCMPP120Dlg, CDialog)
//{{AFX_MSG_MAP(CCMPP120Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CCMPP120Dlg message handlersBOOL CCMPP120Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
//CCMPP120Dlg* Dlg;
//Dlg->OnInitDialog;
mHost="127.0.0.1";
mPort=8801;
if(!mSocket.Create())
{
AfxMessageBox("套接字建立错误!");
return TRUE;
}
if(!mSocket.Connect(mHost,mPort))
{
AfxMessageBox("连接不到服务器!");
return TRUE;
}
////////////////////////////////////////////////////////////
在这里能传入窗体吗?
AfxBeginThread(SocketThread,&mSocket);
///////////////////////////////////////////////////////////////
//UpdateData(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CCMPP120Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}void CCMPP120Dlg::OnButton1()
{
UpdateData();
mSocket.Send(mText,mText.GetLength());
}
解决方案 »
- 求教:vc纵向合并excel单元格,急盼
- ADO连Oracle有必要为每一张表都创建一个类吗&&程序错误求救~
- 讨论:都能上网的两台计算间的通信问题.参与者有分!!!
- 如何确定读写时的地址
- Com+组件安装后,能用什么API来修改“安全”页里的“授权”选项?
- lihao_ningxia(耗子)在么?小弟有事相求。
- 写一个带界面的DLL被DELPHI调用正常,但DELPHI退出时内存非法操作,请指点.
- 给个LEAPFTP2.72的注册码,多谢!
- 我重载了CListView和CTreeView,想知道Item改变的时候触发的事件
- 程序不进InitInstance()。
- 请问各位大侠,如何将INT的数据转化为CString类的字符串?
- 请问在MFC使用Win API,要不要加入什么头文件?
AfxBeginThread(SocketThread,this);
CSocket* mSock=(CSocket*)pParam
这样行吗?Kummer(Kummer) 说得是对的。
关键是你这里错了