📄 writefilethd.cpp
字号:
// WriteFileThd.cpp : implementation file
//
#include "stdafx.h"
#include "scanstringtool.h"
#include "WriteFileThd.h"
#include "FileLine.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CWriteFileThd
IMPLEMENT_DYNAMIC(CWriteFileThd, CWinThread)
CWriteFileThd::CWriteFileThd(CReplaceThd* pRepThd ,CReadScanBaseThd* pScan)
{
m_bAutoDelete = FALSE;
m_hEventKill = CreateEvent(NULL, TRUE, FALSE, NULL);
m_hEventDead = CreateEvent(NULL, TRUE, FALSE, NULL);
m_pRepThd = pRepThd;
m_pScan = pScan;
}
CWriteFileThd::~CWriteFileThd()
{
CloseHandle(m_hEventKill);
CloseHandle(m_hEventDead);
}
BOOL CWriteFileThd::InitInstance()
{
// TODO: perform and per-thread initialization here
DWORD ret;
HANDLE Handles[] = { m_hEventKill,
m_pRepThd->m_hWrite };
FILE* fp = NULL;
char szTmpFilep[4096];
szTmpFilep[0] = 0;
for (;;)
{
ret = WaitForMultipleObjects(2, Handles, false, INFINITE);
if (ret == WAIT_OBJECT_0 ) // End Task
{
VERIFY(SetEvent(m_hEventDead));
break;
}
else if (ret == WAIT_OBJECT_0+1 ) // Receive start task Event
{
if (!theApp.m_fileList.IsEmpty())
{
// FileLine lastLine;
FileLine fileWrite;
theApp.m_CS.Lock();
FileLine* fileLine = (FileLine*) theApp.m_fileList.GetHead();
theApp.m_fileList.RemoveHead();
fileWrite = *fileLine;
delete fileLine;
theApp.m_CS.Unlock();
if(fileWrite.m_bLineType == LTYPE_START)
{
sprintf(szTmpFilep,"%s_tmp",fileWrite.m_szFileName);
fp = fopen(szTmpFilep,"a");
ASSERT( fp !=NULL );
}
else if(fileWrite.m_bLineType == LTYPE_END)
{
ASSERT( fp !=NULL );
fclose(fp); //关闭文件
fp = NULL;
BOOL bRet = CopyFile(szTmpFilep,fileWrite.m_szFileName,false);
if(!bRet)
{
CString strhint;
//
strhint.Format("%s--替换失败,请检查!");
m_pRepThd->m_hDlg->m_ctlDsp.SetWindowText(strhint);
}
else
{
try{
CFile::Remove(szTmpFilep);
}
catch(CFileException e)
{
}
}
szTmpFilep[0] = 0;
SetEvent(m_pScan->m_hEventRead);//文件替换结束
}
else
{
if(fp !=NULL)
{
if(fileWrite.m_bLineType == LTYPE_DEF_NOTE)
{
fprintf(fp,"%s\r\n",fileWrite.m_szLineBuf);
}
else
{
fprintf(fp,"%s",fileWrite.m_szLineBuf);
}
} //end if(fp !=NULL)
}
//lastLine = fileWrite;
} //end if (!theApp.m_fileList.IsEmpty())
else
{
ResetEvent(m_pRepThd->m_hWrite);
}
}
}
return TRUE;
}
int CWriteFileThd::ExitInstance()
{
// TODO: perform any per-thread cleanup here
return CWinThread::ExitInstance();
}
BEGIN_MESSAGE_MAP(CWriteFileThd, CWinThread)
//{{AFX_MSG_MAP(CWriteFileThd)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CWriteFileThd message handlers
void CWriteFileThd::KillThread()
{
VERIFY(SetEvent(m_hEventKill));
// allow thread to run at higher priority during kill process
SetThreadPriority(THREAD_PRIORITY_BELOW_NORMAL);
WaitForSingleObject(m_hEventDead, 2000);
WaitForSingleObject(m_hThread, 1000);
delete this;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -