首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

ping程序在线程函数里总是出现变量指针为null

2012-10-19 
ping程序在线程函数里老是出现变量指针为null定义的变量public:CEdit m_ediAddressCListBox m_lboxRecord

ping程序在线程函数里老是出现变量指针为null
定义的变量
public:
CEdit m_ediAddress;



CListBox m_lboxRecord;
afx_msg void OnPingbutton();


afx_msg void OnStopbutton();
afx_msg LRESULT OnPing(WPARAM wParam, LPARAM lParam);
CString m_strAddress;
CString m_strPing;
CString m_strReply;
CString m_strCome;
CString m_strStatistics;

CWnd*m_pMainWnd;
CWinThread *m_pThread;
boolm_bExitThread;
UINT m_iBagSize;
UINT m_iTimeOut;
UINT m_iTryTimes;
private:

};
struct INFORMATION {
CString *S_pstrPing;
CString *S_pstrReply;
CString *S_pstrCome;
CString *S_pstrStatistics;
UINT *S_piTimeout;
UINT *S_piBagSize;
UINT *S_piTryTimes;
CString *S_pstrAddress;
CWnd*S_pMainWnd;
bool*S_pbExitThread;
};

下面是对onpingbutton的响应和线程函数
void CMyPingDlg::OnPingbutton()
{
UpdateData();

m_ediAddress.GetWindowText(m_strAddress);
::MessageBox(NULL,m_strAddress.GetBuffer(),NULL,0);
void *p;
INFORMATION Struct_information;
Struct_information.S_pMainWnd = (CWnd*)this;
Struct_information.S_pstrAddress =&m_strAddress; 
Struct_information.S_piTryTimes = &m_iTryTimes;
Struct_information.S_piTimeout = &m_iTimeOut;
Struct_information.S_piBagSize = &m_iBagSize;
Struct_information.S_pstrPing = &m_strPing;
Struct_information.S_pstrReply = &m_strReply;
Struct_information.S_pstrCome = &m_strCome;
Struct_information.S_pstrStatistics = &m_strStatistics;
Struct_information.S_pbExitThread = &m_bExitThread;
p = &Struct_information;

m_pThread = AfxBeginThread(&CMyPingDlg::MyThread,p);
if(!m_pThread)
return;
CButton *button = (CButton*)GetDlgItem(IDC_PINGBUTTON);
if (button) button->EnableWindow(FALSE);
//注释这个会出现问题的可能性好大
//不注释这个出现的可能性比较小
Sleep(1);
//button->EnableWindow(TRUE);


}

void CMyPingDlg::OnStopbutton()
{
// TODO: 在此添加控件通知处理程序代码
m_bExitThread = true;
}



[解决办法]
你的代码中Struct_information为局部变量,在函数执行完后就释放了,而你线程中的其他代码还在执行,可以new一个指针给线程使用,线程退出前delete该指针,或将该变量放到全局中去

热点排行