The function Searchfile (path: string) : TStringList; Var SearchRec: TSearchRec; Found: integer; The begin Result:=TStringList. Create; Found:=FindFirst (path + '\' + '*.txt', faAnyFile, SearchRec); While the found=0 do The begin If (SearchRec. Name & lt;> '. ') and (SearchRec Name & lt;> '.. ') and (SearchRec Attr & lt;> Then faDirectory) Result. The Add (path + '\' + SearchRec. Name); Found:=FindNext (SearchRec); end; FindClose (SearchRec); end; API CreateThread (nil, 0, @ thtest, nil, 0, ID); Create a thread
Thtest function following
The function thtest () : string; Var I: Integer; Var STR: string; The list: TStringList; The begin For I:=0 to 1 do The begin Try List:=TStringList. Create; List:=Searchfile (' E: \ Doc \ docs \ sp '); The finally List. Free; end; end; end; Run under the thread tip memory access errors and not wrong every time,
CodePudding user response:
Threads do not use cycle
CodePudding user response:
reference 1st floor ldjssoft response: thread don't use cycle Why is that? The problem is to circulate CodePudding user response:
Sleep under CodePudding user response:
The reference 3 floor flowerslike response: sleep under The same will hang CodePudding user response:
The function thtest () : string; Var I: Integer; Var STR: string; The list: TStringList; The begin For I:=0 to 1 do The begin Try List:=TStringList. Create; List:=Searchfile (' E: \ Doc \ docs \ sp '); //problem here, search code disk file, time consuming, can not performed, is executed next, premature release of the List, there is an error The finally List. Free; end; end; end; Release are using the List of threads appear freezes. CodePudding user response:
First of all, you this list:=TStringList. Create; Created is meaningless, because Searchfile (' E: \ Doc \ docs \ sp ') is returned by the TStringList, behind you the List. The release of Free created without a little to do with you, and second, your statement structure is wrong, the thread function the thread function structure should be similar to the following Procedure thtest (PMyParam: Pointer); Stdcall; Rather than the way you, specific can refer to the MSDN, use you like that, is no guarantee that the thread execution success every time (sometimes successful, sometimes failure error is the problem), in addition you the memory leak a lot of You write the code that is non-standard, correct term should be the following Procedure Searchfile (path: string; List: TStringList); Var SearchRec: TSearchRec; Found: integer; The begin List. The Clear; Found:=FindFirst (path + '\' + '*.txt', faAnyFile, SearchRec); While the found=0 do The begin If (SearchRec. Name & lt;> '. ') and (SearchRec Name & lt;> '.. ') and (SearchRec Attr & lt;> Then faDirectory) List. The Add (path + '\' + SearchRec. Name); Found:=FindNext (SearchRec); end; FindClose (SearchRec); end; Procedure thtest (PMyParam: Pointer); Stdcall; Var I: Integer; Var STR: string; The list: TStringList; The begin List:=PMyParam; Searchfile (' E: \ Doc \ docs \ sp ', a List). end; Var ID: DWORD; Lst: TStringList; Handle: THandle; The begin Lst:=TStringList. Create; Handle:=CreateThread (nil, 0, @ thtest, Lst, 0, ID); The WaitForSingleObject (handle, INFINITE); The CloseHandle (Handle); ShowMessage (Lst) Text); Lst. Free; CodePudding user response:
refer to 6th floor suiyunonghen response: first of all, do you have in this list:=TStringList. Create; Created is meaningless, because Searchfile (' E: \ Doc \ docs \ sp ') is returned by the TStringList, behind you the List. The release of Free created without a little to do with you, and second, your statement structure is wrong, the thread function the thread function structure should be similar to the following Procedure thtest (PMyParam: Pointer); Stdcall; Rather than the way you, specific can refer to the MSDN, use you like that, is no guarantee that the thread execution success every time (sometimes successful, sometimes failure error is the problem), in addition you the memory leak a lot of You write the code that is non-standard, correct term should be the following Procedure Searchfile (path: string; List: TStringList); Var SearchRec: TSearchRec; Found: integer; The begin List. The Clear; Found:=FindFirst (path + '\' + '*.txt', faAnyFile, SearchRec); While the found=0 do The begin If (SearchRec. Name & lt;> '. ') and (SearchRec Name & lt;> '.. ') and (SearchRec Attr & lt;> Then faDirectory) List. The Add (path + '\' + SearchRec. Name); Found:=FindNext (SearchRec); end; FindClose (SearchRec); end; Procedure thtest (PMyParam: Pointer); Stdcall; Var I: Integer; Var STR: string; The list: TStringList; The begin List:=PMyParam; Searchfile (' E: \ Doc \ docs \ sp ', a List). end; Var ID: DWORD; Lst: TStringList; Handle: THandle; The begin Lst:=TStringList. Create; Handle:=CreateThread (nil, 0, @ thtest, Lst, 0, ID); The WaitForSingleObject (handle, INFINITE); The CloseHandle (Handle); ShowMessage (Lst) Text); Lst. Free; The WaitForSingleObject (handle, INFINITE); A thread is waiting for that only one thread is not going to hang you remove wait will hang CodePudding user response:
reference 5 floor lyhoo163 reply: function thtest () : string; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull