我在一个workthread中对一个文件有操作,但总是出现异常
后来跟踪发现 在线程函数运行中即使我手动删除了文件,
workthread函数中的GetFileAttributes()函数居然还判定文件存在
其他比如CStdioFile类中的SeekToEnd函数也反映不正常
我用这个同步,实际在workthread运行中,界面上都没操作那个文件
EnterCriticalSection(&g_logsec);
LeaveCriticalSection(&g_logsec);谁帮我解释一下??
后来跟踪发现 在线程函数运行中即使我手动删除了文件,
workthread函数中的GetFileAttributes()函数居然还判定文件存在
其他比如CStdioFile类中的SeekToEnd函数也反映不正常
我用这个同步,实际在workthread运行中,界面上都没操作那个文件
EnterCriticalSection(&g_logsec);
LeaveCriticalSection(&g_logsec);谁帮我解释一下??
实际就是写日志
EnterCriticalSection(&g_logsec);
DWORD dwAttr;
CString str;
CTime t=CTime::GetCurrentTime ();
str=t.Format ("%Y-%m-%d %H:%M:%S ");
str = str+strLog+"\r\n";
dwAttr=GetFileAttributes(strFileName);
DWORD dw = GetLastError();
if(dwAttr==0xFFFFFFFF)//如果该文件不存在就创建一个
{
CStdioFile file;
file.Open(strFileName,CFile::modeCreate|CFile::modeWrite);
file.Close();
}
CStdioFile file;
file.Open(strFileName,CFile::modeWrite);
try
{
DWORD dwdd= file.SeekToEnd();
}
catch (CFileException & e)
{
int i;
i=e.m_cause;
}
file.WriteString(str);
file.Close(); LeaveCriticalSection(&g_logsec);
return;
我把这个代码拿出来测试,确实也没问题
但是一到我程序里面问题就来了,我郁闷啊
我在界面中也是调用这个函数。就是说我所有的关于文件的操作都包括在临界区中的,怎么会有错呢
这不会有错吧!
原因终于找到了
我的程序中用到了一个数据库类,是我自己写的
我给类设计了两个连接对象,一个用在workthread中,一个用在界面中
我发现只要一用界面的连接插入数据时,workthread就出现莫名其妙的异常,导致我对文件的操作也产生了异常,而且很难发现(workthread中用另外一个连接访问数据库),我手动在数据库中插入数据库时,workthread就跑的很正常。。大家下次设计数据库类的时候要小心了,特别是用两个连接的时候要小心
再次感谢 jasonshark