THashedStringList和TStringList在多线程里面表现怎么这么奇怪?

dance_code 2008-03-15 03:22:46
多线程不是第一次写了,主要是照顾用户使用的方便,不会让主窗口停顿
这次用到了THashedStringList以及TStringList处理上万条文本行的搜索和更新,也是利用多线程,主窗口只是个进度条走动而已
但是当线程执行以后,主窗口不能拖动也不能获得焦点,只能看到进度条在走动,而其除了主菜单还能显示出来,其他的控件都消失了。等线程执行完毕了才又出来,好像是重新绘制了窗口。
我在线程里面也用了Synchronize来同步这个过程,但是没有任何效果。
请问各位有没有遇到类似的问题?我错在哪里?
下面是线程中的过程,而且我发现处理小数据量的时候一点问题没有,或许是太快感觉不到。
var
TstringTmp01,TstringTmp02:THashedStringList;//
intTmp01,intTmp02,intIndex:integer;//计数器
StrSource,StrCheck:string; //源字符和对比字符
int_Temp:integer;
begin
TstringTmp01:=THashedStringList.Create;
TstringTmp02:=THashedStringList.Create;
TstringTmp01.LoadFromFile('20080227123152.txt');
label1.Caption:=inttostr(TstringTmp01.Count);
TstringTmp01.Sort;
// TstringTmp01.Sorted:=true;
int_Temp:=0;
for intTMp01 := 0 to TstringTmp01.Count-1 do begin
strSource:=TstringTmp01[intTmp01];
if (length(strSource)>0) then begin
while (TstringTmp01.Find(strSource,intTmp02))do
TstringTmp01[intTmp02]:='';
if not TstringTmp02.Find(strSource,int_Temp) then TstringTmp02.Add(strSource);
end;
end;


for intTmp02 := 0 to TstringTmp02.Count - 1 do
memo2.Lines.Add(TstringTmp02[intTmp02]);
TstringTmp01.Free;
TstringTmp02.Free;
...全文
192 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
songyan 2008-03-15
  • 打赏
  • 举报
回复
使用Synchronize的话其实就是单线程执行。你应该只把不是线程安全的部分用Synchronize来执行。看你的程序,应该把
for intTmp02 := 0 to TstringTmp02.Count - 1 do
memo2.Lines.Add(TstringTmp02[intTmp02]);
这几行放在一个procedure里,然后用Synchronize来执行这个procedure。
dance_code 2008-03-15
  • 打赏
  • 举报
回复
谢谢楼上的提醒,受教了,已经圆满解决

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧