一、在对话框的OnInitDialog中创建接受数据的文件,并启动AutoSaveData线程
1 CTime t = CTime::GetTickCount(); 2 CString str = t.Format(_T("%Y-%m-%d")); 3 if(!m_rFile.Open(m_strCurPath+_T("\\")+str+_T(".txt"),CFile::modeCreate | CFile::modeWrite |CFile::modeNoTruncate)) 4 { 5 AfxMessageBox( "创建记录文件失败!"); 6 m_brFileOpen = FALSE; 7 }else{ 8 m_brFileOpen = TRUE; 9 } 10 11 m_Thread = AfxBeginThread(AutoSaveData,this);
二、线程方法实现如下
1 UINT CSCOMMDlg::AutoSaveData(LPVOID pParam) 2 { 3 CSCOMMDlg* pDlg = (CSCOMMDlg*)pParam; 4 5 CString str; 6 int currentPoint = 0; 7 while(!pDlg->m_bExitThread) 8 { 9 if(pDlg->sourceUnChar[0]) 10 { 11 CTime t = CTime::GetCurrentTime(); 12 CString sTime = t.Format("%Y-%m-%d %H:%M:%S "); 13 str = sTime + str; 14 bool check = TRUE; 15 while(check) 16 { 17 check = FALSE; 18 while(pDlg->sourceUnChar[currentPoint]) 19 { 20 check = TRUE; 21 str += pDlg->sourceUnChar[currentPoint]; 22 currentPoint++; 23 } 24 Sleep(20); 25 } 26 currentPoint = 0; 27 pDlg->m_countPackage = 0; 28 memset(pDlg->sourceUnChar,0,BUFFERE_LEN*sizeof(char)); 29 str += _T("\r\n"); 30 if (pDlg->m_brFileOpen) 31 { 32 pDlg->m_rFile.SeekToEnd(); 33 pDlg->m_rFile.Write(str,str.GetLength()); 34 pDlg->m_rFile.Flush(); 35 }else{ 36 CTime t = CTime::GetTickCount(); 37 CString str = t.Format(_T("%Y-%m-%d")); 38 if(!pDlg->m_rFile.Open(pDlg->m_strCurPath+_T("\\")+str+_T(".txt"),CFile::modeCreate | CFile::modeWrite |CFile::modeNoTruncate)) 39 { 40 AfxMessageBox( "创建记录文件失败!"); 41 pDlg->m_brFileOpen = FALSE; 42 }else{ 43 pDlg->m_brFileOpen = TRUE; 44 } 45 } 46 str.Empty(); 47 } 48 } 49 return 0; 50 }
时间: 2024-11-05 23:27:18