本文介绍了完成任务后运行线程的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 请检查以下代码: 头文件:Please check the code below:header file:#pragma once#include "ColoredListCtrl.h"// CChildView windowclass CPhoneVerificationView : public CWnd{private:CColoredListCtrl m_Grid;CButton m_OpenFile,btn2_export_excel;CString m_sFileName;CEdit m_FilePathCtrl, m_FileLeft;INT UnderProcess, CantClose;CWinThread *m_cwt;CFileDialog *fOpenDlg, *PhoneList;wchar_t* pBuffer;// Constructionpublic:CPhoneVerificationView();// Attributespublic:// Operationspublic:// Overridesprotected:virtual BOOL PreCreateWindow(CREATESTRUCT& cs);// Implementationpublic:virtual ~CPhoneVerificationView();void LoadCDR();static UINT StaticLoadCDR(LPVOID lp);// Generated message map functionsprivate:void LoadPhoneList();INT GetPhoneNumberRow(CString);protected:afx_msg void OnPaint();afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);afx_msg void OnSize(UINT, int, int);afx_msg void SaveOnExcelThread();void CreateHeader();void GetFileName();void CreateThreadFor();DECLARE_MESSAGE_MAP()};void CPhoneVerificationView::GetFileName(){if(UnderProcess){MessageBox(L"One Process is running already");return;}PhoneList=new CFileDialog(TRUE);//, L"", L"", OFN_HIDEREADONLY|OFN_FILEMUSTEXIST|OFN_ALLOWMULTISELECT,//L"Text File (*.txt)|*.txt|*.dat|*.csv||", this);pBuffer = new wchar_t[24576]; // 24K memset(pBuffer,0, sizeof (pBuffer));//fOpenDlg->SetWindowText(L"Select the Phone List");PhoneList->m_ofn.lStructSize=sizeof OPENFILENAME;PhoneList->m_ofn.Flags |= OFN_HIDEREADONLY|OFN_FILEMUSTEXIST;//|OFN_ALLOWMULTISELECT; PhoneList->m_ofn.lpstrFile = pBuffer;PhoneList->m_ofn.nMaxFile = 24576;PhoneList->m_ofn.hwndOwner = this->GetSafeHwnd();PhoneList->m_ofn.lpstrFilter = L"CSV File (*.csv)|*.csv|Text File(*.txt)|*.txt|All Files (*.*)|*.*||";;PhoneList->m_ofn.nFilterIndex =1;PhoneList->m_ofn.lpstrFileTitle = NULL ;PhoneList->m_ofn.nMaxFileTitle = 0 ;PhoneList->m_ofn.lpstrInitialDir=NULL ;int ret=PhoneList->DoModal();ASSERT( ( ret != IDOK ) || ( ret != IDCANCEL ) );if(ret==IDCANCEL){delete PhoneList;delete []pBuffer;return;}LoadPhoneList();delete PhoneList; //no more usedelete []pBuffer; //no more usefOpenDlg=new CFileDialog(TRUE);//, L"", L"", OFN_HIDEREADONLY|OFN_FILEMUSTEXIST|OFN_ALLOWMULTISELECT,//L"Text File (*.txt)|*.txt|*.dat|*.csv||", this);pBuffer = new wchar_t[24576]; // 24K memset(pBuffer,0, sizeof (pBuffer));//fOpenDlg->SetWindowText(L"Select the Phone List");fOpenDlg->m_ofn.lStructSize=sizeof OPENFILENAME;fOpenDlg->m_ofn.Flags |= OFN_HIDEREADONLY|OFN_FILEMUSTEXIST|OFN_ALLOWMULTISELECT; fOpenDlg->m_ofn.lpstrFile = pBuffer;fOpenDlg->m_ofn.nMaxFile = 24576;fOpenDlg->m_ofn.hwndOwner = this->GetSafeHwnd();fOpenDlg->m_ofn.lpstrFilter = L"CSV File (*.csv)|*.csv|Text File(*.txt)|*.txt|All Files (*.*)|*.*||";;fOpenDlg->m_ofn.nFilterIndex =1;fOpenDlg->m_ofn.lpstrFileTitle = NULL ;fOpenDlg->m_ofn.nMaxFileTitle = 0 ;fOpenDlg->m_ofn.lpstrInitialDir=NULL ;//intret=fOpenDlg->DoModal();ASSERT( ( ret != IDOK ) || ( ret != IDCANCEL ) );if(ret==IDOK){CreateThreadFor();}else{delete []fOpenDlg->m_ofn.lpstrFile;delete fOpenDlg;fOpenDlg=NULL;}}void CPhoneVerificationView::CreateThreadFor(){m_cwt=AfxBeginThread(StaticLoadCDR,this,THREAD_PRIORITY_NORMAL,0,0,NULL);}UINT CPhoneVerificationView::StaticLoadCDR(LPVOID lp){CPhoneVerificationView *proc=(CPhoneVerificationView *)lp;proc->LoadCDR();return 1;}void CPhoneVerificationView::LoadCDR(){if(UnderProcess){MessageBox(L"Once Process is running already");return;}UnderProcess=1;int i = 0;CString Today;time_t ti;memset(&ti, 0, sizeof time_t);time(&ti); // get time nowstruct tm *now = localtime(&ti);Today.Format(L"%d-%0d-%0d", now->tm_year+1900, now->tm_mon+1, now->tm_mday);UnderProcess=1;CantClose=1;CMySQLEx conn;int count=0;int row_count = 0U;int FileCount=0;while(UnderProcess && FileCount+1<=m_FileList.size()){FileCount++;CString str,ret;str.Format(L"%d", FileCount);m_FileLeft.SetWindowText(str);m_sFileName=m_FileList[FileCount-1];const char field_terminator = '\t';const char line_terminator = '\n';const char enclosure_char = '"';csv_parser CDRParser(m_sFileName.GetBuffer());m_sFileName.ReleaseBuffer();CDRParser.SetSkipLine(0);CDRParser.SetEnclosedChar(enclosure_char);CDRParser.SetFieldTerminator(field_terminator);CDRParser.SetLineTerminator(line_terminator);CDRParser.SetEscapeChar('\\');/* Check to see if there are more records, then grab each row one at a time */CStringA Query;double TotalCharge=0;while(CDRParser.has_more_rows()){count++;struct RET_CSV *row;int ColumnCount;row = CDRParser.GetNextRow(&ColumnCount);if(ColumnCount==0)continue;if(strcmp("WillCom WVPN", row[3].Value)!=0)continue;int RowNumber=GetPhoneNumberRow(CFunctions::UTF8toUTF16(row[6].Value));if(RowNumber==-1)continue;str=m_Grid.GetItemText(RowNumber, 3);CString Date;Date.Format(L"%c%c%c%c-%c%c-%c%c", row[0].Value[0],row[0].Value[1],row[0].Value[2],row[0].Value[3],row[0].Value[4],row[0].Value[5],row[0].Value[6],row[0].Value[7]);int cmp=wcscmp(Date,str.GetBuffer());//str.Compare(Date);str.ReleaseBuffer();if(cmp<=0)continue;m_Grid.SetItemText(RowNumber, 3, Date);str=CFunctions::UTF8toUTF16(row[1].Value);m_Grid.SetItemText(RowNumber, 4, str);long notused=CFunctions::DateDifferenct(Today, Date);str.Format(L"%ld", notused);m_Grid.SetItemText(RowNumber, 6, str);}}UnderProcess=0;//delete []PhoneList->m_ofn.lpstrCustomFilter;//delete PhoneList;MessageBox(L"Process is Done");m_cwt->m_bAutoDelete = TRUE;AfxEndThread(0);} 问题是我关闭应用程序后它并没有安静下来。它一直在运行。 这是我在任务管理器中看到的一些信息。 时间 线程数 在我开始这个过程之前 6 在我开始这个过程之后(开头) 12 我开始这个过程后(一段时间后) 8 完成该过程后 7 所以,我所看到的最后,一个进程仍在运行。即使我关闭窗口仍然有一个线程正在运行,你能建议我该怎么做才能找出哪个正在运行?The problem is after I close the application it is not quieting. It keeps running.Here is some information that I saw in task manager.TimeNumber of threadBefore I start the process6After I start the process(at the beginning)12After I start the process(after some time)8After The process is done7So, What I see at the end one process is still running. Even if I close the window still there is one thread running, can you suggest what shall i do to figure out which is running?推荐答案引用:要结束线程,请从线程内调用AfxEndThread,或者从工作线程的控制函数返回。To end the thread, call AfxEndThread from within the thread, or return from the controlling function of the worker thread. 看看你的设计,我得问你为什么要使用线程。您正在以不安全的方式从工作线程访问this指针。如果你创建了多个线程,他们都可以在主线程中使用你的类的成员数据。 你的设计很容易失败并且会呈指数级增长。 MFC MDI文档/视图架构更适合您的设计。想象一下你的每个线程作为文档/视图对。主要信息可以存储在你的CWinApp派生类中。 我不是想对你努力,但如果你真的想做线程那么派生一个来自CWinThread的类,并使用PostThreadMessage与之通信。我有一个多线程的MFC MDI文档/视图文章,真正解释了如何派生线程并使用它们。您应该查看我的文章的那一部分,并在临时应用程序中进行一些代码复制/粘贴,以了解制作和使用线程。Looking at your design, I gotta ask why are you using threads. You are accessing your ''this'' pointer in an unsafe way from a worker thread. If you have multiple threads created they could all be jacking with the member data of your class in the main thread.Your design is prone to fail and grow exponentially complicated.A MFC MDI document/view architecture is much better suited for your design. Imagine each one of your threads as a document/view pair. The main information can be stored in your CWinApp derived class.I''m not trying to be hard on you but if you really want to do threads then derive a class from CWinThread and communicate to it with PostThreadMessage. I have a multithreaded MFC MDI document/view article that really explains how to derive threads and use them. You should look over that portion of my article and do some code copy/pasting in a scratch app to get an idea about making and using threads.18862040, 220, e:\ryo desktop\ryo desktop\csv\csv_parser.cpp, buff18730496, 220, e:\ryo desktop\ryo desktop\csv\csv_parser.cpp, buff18729816, 220, e:\ryo desktop\ryo desktop\csv\csv_parser.cpp, buff18808512, 220, e:\ryo desktop\ryo desktop\csv\csv_parser.cpp, buff18802840, 220, e:\ryo desktop\ryo desktop\csv\csv_parser.cpp, buff18729880, 220, e:\ryo desktop\ryo desktop\csv\csv_parser.cpp, buff18802904, 220, e:\ryo desktop\ryo desktop\csv\csv_parser.cpp, buff18735864, 220, e:\ryo desktop\ryo desktop\csv\csv_parser.cpp, buff 通过这个,我发现我的一个动态变量根本没有释放,我找出原因并修复它。现在,它的工作正常:)谢谢大家的支持through this, I figured out that one of my dynamic variable is not freeing at all, I figured out the cause and fixed it. now, Its working fine :) Thank you everybody for your support 这篇关于完成任务后运行线程的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!