列出关键的, 其他多余的未考
FindStr.h
/////////////////////////////////////////
class CFindStr : public CWinThread
{
DECLARE_DYNCREATE(CFindStr)
public:
CFindStr(); // protected constructor used by dynamic creation
virtual ~CFindStr();
// Attributes
public:
void setBackup(BOOL bBackup); //±¸·Ý
void setCaseSensible(BOOL bCase); //´óСдÃô¸Ð
void setDir(CString sDir); //²éÕÒ·¾¶
void setLookfor(CString sLookfor); //²éÕÒ´®
void setReplace(CString sReplace); //¸ü¸Ä´®
void setExType(CString sType); //ÎļþÀ©Õ¹Ãû
void setWnd(CWnd* pWnd); //Ö÷´°¿ÚÖ¸Õë
private:
BOOL m_bBackup;
BOOL m_bCase;
CString m_sDir;
CString m_sLookfor;
CString m_sReplace;
CString m_sType;
CListBox m_lb;
CWnd* m_pWnd; WIN32_FIND_DATA filestruct; void doLookFile(CString path);
void doModify();
// Operations
public:// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CFindStr)
public:
virtual BOOL InitInstance();
virtual int ExitInstance();
virtual int Run();
//}}AFX_VIRTUAL// Implementation
protected:
// Generated message map functions
//{{AFX_MSG(CFindStr)
// NOTE - the ClassWizard will add and remove member functions here.
//}}AFX_MSG DECLARE_MESSAGE_MAP()
};
/////////////////////////////////////////////////////
FindStr.cpp
/////////////////////////////////////////////////////
int CFindStr::Run()
{
doModify();
return CWinThread::Run();
}void CFindStr::setBackup(BOOL bBackup)
{
m_bBackup = bBackup;
}void CFindStr::setCaseSensible(BOOL bCase)
{
m_bCase = bCase;
}void CFindStr::setWnd(CWnd* pWnd)
{
m_pWnd = pWnd;
}void CFindStr::setDir(CString sDir)
{
m_sDir = sDir;
}void CFindStr::setLookfor(CString sLookfor)
{
m_sLookfor = sLookfor;
}void CFindStr::setReplace(CString sReplace)
{
m_sReplace = sReplace;
}void CFindStr::setExType(CString sType)
{
m_sType = sType;
}void CFindStr::doLookFile(CString path)
{
CFileFind find;
CString _path = path; if(_path.Right(1)!="\\")
_path+="\\"; _path+="*.*";
BOOL ret = find.FindFile(_path);
while(ret)
{
ret = find.FindNextFile();
if(find.IsDots())
continue; if(find.IsDirectory())
{
doLookFile(find.GetFilePath());
}
else
((CListBox*)(GetMainWnd()->GetDlgItem(IDC_LISTFILE)))->AddString(find.GetFilePath());
}
find.Close();
}void CFindStr::doModify()
{
///get single file path
CString sfilePath; doLookFile(m_sDir);
}
///////////////////////////////////////////////////////
在ExStrDlg中引用这个线程类
ExStrDlg.h
/////////////////////////////
#include "FindStr.h"
.....
CFindStr* m_pFindStr;
...
///////////////////
ExStrDlg.cpp
//////////////////
CExStrDlg::CExStrDlg(CWnd* pParent /*=NULL*/)
: CDialog(CExStrDlg::IDD, pParent)
{
.....
m_findstr = new CFindStr();
}void CExStrDlg::OnBtnStart()
{
....
m_findstr->Run();
}
void CExStrDlg::OnBtnEnd()
{
DWORD dw = 0;
GetExitCodeThread(m_findstr->m_hThread, &dw);
TerminateThread(m_findstr->m_hThread, dw);
delete m_findstr; //有错误发生--访问非法内存!!!!
m_findstr = NULL;
}
void CExStrDlg::OnDestroy()
{
CDialog::OnDestroy(); if(m_findstr != NULL)
{
DWORD dw = 0; GetExitCodeThread(m_findstr->m_hThread, &dw);
TerminateThread(m_findstr->m_hThread, dw);
delete m_findstr;////////在这个函数里执行没有错误!!!!
m_findstr = NULL;
}
}
////////////////////////////////////////
大概就是这样. 我运行的时候,如果文件多,界面停止不动,与没有使用线程一样!11希望高手高手高高手都来回答, 新手新手新新手也来学学东西!
FindStr.h
/////////////////////////////////////////
class CFindStr : public CWinThread
{
DECLARE_DYNCREATE(CFindStr)
public:
CFindStr(); // protected constructor used by dynamic creation
virtual ~CFindStr();
// Attributes
public:
void setBackup(BOOL bBackup); //±¸·Ý
void setCaseSensible(BOOL bCase); //´óСдÃô¸Ð
void setDir(CString sDir); //²éÕÒ·¾¶
void setLookfor(CString sLookfor); //²éÕÒ´®
void setReplace(CString sReplace); //¸ü¸Ä´®
void setExType(CString sType); //ÎļþÀ©Õ¹Ãû
void setWnd(CWnd* pWnd); //Ö÷´°¿ÚÖ¸Õë
private:
BOOL m_bBackup;
BOOL m_bCase;
CString m_sDir;
CString m_sLookfor;
CString m_sReplace;
CString m_sType;
CListBox m_lb;
CWnd* m_pWnd; WIN32_FIND_DATA filestruct; void doLookFile(CString path);
void doModify();
// Operations
public:// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CFindStr)
public:
virtual BOOL InitInstance();
virtual int ExitInstance();
virtual int Run();
//}}AFX_VIRTUAL// Implementation
protected:
// Generated message map functions
//{{AFX_MSG(CFindStr)
// NOTE - the ClassWizard will add and remove member functions here.
//}}AFX_MSG DECLARE_MESSAGE_MAP()
};
/////////////////////////////////////////////////////
FindStr.cpp
/////////////////////////////////////////////////////
int CFindStr::Run()
{
doModify();
return CWinThread::Run();
}void CFindStr::setBackup(BOOL bBackup)
{
m_bBackup = bBackup;
}void CFindStr::setCaseSensible(BOOL bCase)
{
m_bCase = bCase;
}void CFindStr::setWnd(CWnd* pWnd)
{
m_pWnd = pWnd;
}void CFindStr::setDir(CString sDir)
{
m_sDir = sDir;
}void CFindStr::setLookfor(CString sLookfor)
{
m_sLookfor = sLookfor;
}void CFindStr::setReplace(CString sReplace)
{
m_sReplace = sReplace;
}void CFindStr::setExType(CString sType)
{
m_sType = sType;
}void CFindStr::doLookFile(CString path)
{
CFileFind find;
CString _path = path; if(_path.Right(1)!="\\")
_path+="\\"; _path+="*.*";
BOOL ret = find.FindFile(_path);
while(ret)
{
ret = find.FindNextFile();
if(find.IsDots())
continue; if(find.IsDirectory())
{
doLookFile(find.GetFilePath());
}
else
((CListBox*)(GetMainWnd()->GetDlgItem(IDC_LISTFILE)))->AddString(find.GetFilePath());
}
find.Close();
}void CFindStr::doModify()
{
///get single file path
CString sfilePath; doLookFile(m_sDir);
}
///////////////////////////////////////////////////////
在ExStrDlg中引用这个线程类
ExStrDlg.h
/////////////////////////////
#include "FindStr.h"
.....
CFindStr* m_pFindStr;
...
///////////////////
ExStrDlg.cpp
//////////////////
CExStrDlg::CExStrDlg(CWnd* pParent /*=NULL*/)
: CDialog(CExStrDlg::IDD, pParent)
{
.....
m_findstr = new CFindStr();
}void CExStrDlg::OnBtnStart()
{
....
m_findstr->Run();
}
void CExStrDlg::OnBtnEnd()
{
DWORD dw = 0;
GetExitCodeThread(m_findstr->m_hThread, &dw);
TerminateThread(m_findstr->m_hThread, dw);
delete m_findstr; //有错误发生--访问非法内存!!!!
m_findstr = NULL;
}
void CExStrDlg::OnDestroy()
{
CDialog::OnDestroy(); if(m_findstr != NULL)
{
DWORD dw = 0; GetExitCodeThread(m_findstr->m_hThread, &dw);
TerminateThread(m_findstr->m_hThread, dw);
delete m_findstr;////////在这个函数里执行没有错误!!!!
m_findstr = NULL;
}
}
////////////////////////////////////////
大概就是这样. 我运行的时候,如果文件多,界面停止不动,与没有使用线程一样!11希望高手高手高高手都来回答, 新手新手新新手也来学学东西!
似乎不应这样结束线程,你应当由主线程向他发一个消息,他收到这个消息后,用AfxEndThread()结束。