0
請幫我理解我的代碼有什麼問題。AfxBeginThread()+ Cstring =垃圾內容
頭文件
typedef void (*pStatusCallback)(UINT code, const CString& message);
class CComunicator
{
private:
CUT_WSClient _client;
bool _shouldTerminate;
CString _serverAddress;
UINT _serverPort;
pStatusCallback _statusCallback;
UINT _ThreadFunc();
static UINT ThreadFunc(LPVOID pParam);
public:
CComunicator(const CString& serverAddress, UINT serverPort, pStatusCallback statusCallback);
~CComunicator(void);
void Start();
void Stop();
}
源文件
CComunicator::CComunicator(const CString& serverAddress, UINT serverPort, pStatusCallback statusCallback)
{
_serverAddress = serverAddress;
_serverPort = serverPort;
_statusCallback = statusCallback;
}
CComunicator::~CComunicator(void)
{
}
void CComunicator::Start()
{
_shouldTerminate = false;
AfxBeginThread(CComunicator::ThreadFunc, this);
}
void CComunicator::Stop()
{
_shouldTerminate = true;
}
UINT CComunicator::ThreadFunc(LPVOID pParam)
{
return ((CComunicator*)pParam)->_ThreadFunc();
}
UINT CComunicator::_ThreadFunc()
{
_statusCallback(0, _T("Connecting..."));
_client.Connect(_serverPort, _serverAddress);
_statusCallback(0, _T("Connected"));
// do here some work
_client.CloseConnection();
return 0;
}
使用
CComunicator com(_T("10.1.1.105"), 4502, ComunicatorCallback);
com.Start();
爲什麼在方法_ThreadFunc _serverAddress包含垃圾郵件符號? _serverPort具有正確的值? 沒有人正在改變_serverAddress。
Thx任何想法。
OMG,大衛你是對的,之後纔剛到主方法結束:)所以程序就完成了。謝謝! – Eugen