一个多线程和目录监控问题,请高手帮忙看看 (50分)

  • 主题发起人 andy_yans
  • 开始时间
A

andy_yans

Unregistered / Unconfirmed
GUEST, unregistred user!
以下是我的一个线程里的execute代码,strdirectory为要监控的目录<br>case 语句只是用来判断根据我传进的参数修改相应的变量,因为我开三个线程,监视<br>3个目录的,所以用case语句。<br>procedure TMoniterThread.Execute;<br>begin<br> self.FreeOnTerminate:=True;<br>&nbsp; hNotify:=FindFirstChangeNotification(PChar(strDirectory),TRUE,<br>&nbsp; &nbsp; FILE_NOTIFY_CHANGE_SIZE);<br>&nbsp; &nbsp; if hNotify=INVALID_HANDLE_VALUE then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; ShowMessage('监控初始化出错!');<br> Terminate;//停止线程,这样正确吗?<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; while(Not Terminated) do//我改为while(true) do会死掉,所以这样写,不知道对不对<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; if WaitForSingleObject(hNotify,INFINITE)=WAIT_OBJECT_0 &nbsp;then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case iDepartment of<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; conNewsStation:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bNewsStationChanged:=true;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;conEconomyStation:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;bEconomyStationChanged:=True;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;conMusicStation:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;bMusicStationChanged:=True;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br> &nbsp; &nbsp; &nbsp; &nbsp; if FindNextChangeNotification(hNotify)=FALSE then<br>&nbsp; &nbsp; &nbsp; &nbsp;Terminate;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>end;<br>问题是,以上代码在windows 2000里运行,全部正确,结果也正确<br>但是如果在windws98里执行是,就说初始化出错,连续弹出错误代码:<br>无效的句柄参数(87).<br>如果将hNotify:=FindFirstChangeNotification(PChar(strDirectory),TRUE,<br>&nbsp; &nbsp; FILE_NOTIFY_CHANGE_SIZE);<br>该为FALSE,初始化没有出错,但还是弹出同样的抱错,说无效的句柄参数.<br>不知道为什么啊?而且我认为问题在于对WaitForSingleObject(hNotify,INFINITE)<br>函数的调用,因为用了hNotify句柄,但是为什么初始化没有出错,还会有系统报错呢,<br>再说我不是在出错时,Termidate掉线程了吗,下面的代码应该不执行啊.<br>请高手指点?小弟刚刚来,没多少分,拜托各位了.
 
顶部