投稿

ラベル(匿名関数)が付いた投稿を表示しています

Delphi 無名メソッド(無名関数)で簡単スレッド + OnTerminate

無名メソッドで、簡単使い捨て?スレッド Caption := 'Wait...'; TThread.CreateAnonymousThread( procedure begin Sleep(3000); TThread.Synchronize(TThread.CurrentThread, procedure() begin Form1.Caption := 'Done'; end); end).Start; CreateAnonymousThread メソッドは、 Suspend = True FreeOnTerminate = True で、Thread を生成するので、Start メソッドを付けてスレッドを開始させるだけ 【追記】 OnTerminate も無名メソッドで記載してみる type TNotifyEventProc = reference to procedure(Sender: TObject); // ------------- procedure TForm1.Button1Click(Sender: TObject); procedure MethRefToMethPtr(const MethRef; var MethPtr); type TVtable = array [0 .. 3] of Pointer; PVtable = ^TVtable; PPVtable = ^PVtable; begin TMethod(MethPtr).Code := PPVtable(MethRef)^^[3]; TMethod(MethPtr).Data := Pointer(MethRef); end; function MakeNotify(const Proc: TNotifyEventProc): TNotifyEvent; begin MethRefToMethPtr(Proc, Res...

Delphi/C++Builder Generics TList(TObjectList) と無名関数でソートと元の順序

Generics TList と匿名関数のソートメモ TList<THogehoge> を匿名関数でソートする場合 System.Generics.Defaults の TComparer を挟んで、function の const もお忘れ無く uses Generics.Collections, System.Generics.Defaults; ItemList.Sort(TComparer<THogehoge>.Construct( function(const Item1, Item2: THogehoge): Integer begin Result := AnsiCompareStr(Item1.Value, Item2.Value); end)); ※AnsiCompareStr と CompareStr でソート順序が違う場合あり(前者はOS言語設定依存) 【捕捉】 上記はクイックソートで行われるため、Item1 と Item2 の比較が同じだと元の順序が保たれない場合があるので、Compare結果が同じ場合、私は元の順序であるリストインデックスで比較したりしてます // 日付の例 if (Item1.date = Item2.date) then begin Result := ItemList.IndexOf(Item1) - ItemList.IndexOf(Item2); end else begin Result := CompareDate(Item1.date, Item2.date); end;