Delphi:TThreadList有时锁定程序 [英] Delphi: TThreadList sometimes lock program

查看:354
本文介绍了Delphi:TThreadList有时锁定程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时这个功能锁定我的程序,它会冻结,直到我关闭它。
这里有什么问题?

Sometimes this function locks my program, and it's freezes until i close it. What is wrong here ?

function del_from_list(id:string):boolean;
var i : integer;
begin
  Result := True;
  try
    with global_list.LockList do
    begin
      for i:=0 to Count-1 do
      begin
        if Tthread_list(Items[i]).id = id then
        begin
          Delete(i);
          break;
        end;
      end;
    end;
  finally
    global_list.UnlockList;
  end;
end;

课程

  Tthread_list = class
  public
    id   : string;
    constructor Create(const id: string);
  end;

我添加到列表中:

global_list.Add(Tthread_list.Create('xxx'));

全局列表是一个全局变量

global list is a global variable

var global_list : TThreadList = nil;


推荐答案

您需要调用 LockList ()之外尝试块而不是内部,例如:

You need to call LockList() outside of the try block instead of inside of it, eg:

function del_from_list(const id: string): boolean;
var
  List: TList;
  i : integer;
begin
  Result := False;
  List := global_list.LockList;
  try
    with List do
    begin
      for i :=0 to Count-1 do
      begin
        if Tthread_list(Items[i]).id = id then
        begin
          Delete(i);
          Result := True;
          break;
        end;
      end;
    end;
  finally
    global_list.UnlockList;
  end;
end;

这篇关于Delphi:TThreadList有时锁定程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆