Delphi 2010线程

时间:2011-03-08 13:39:03

标签: multithreading delphi delphi-2010

有人可以发布Delphi 2010最简单的线程示例,例如,当点击按钮时,会将一些文本放入备忘录中吗?随着实施和所有。感谢。

更新:好的,只是一个做某事的简单线程。不需要备忘录。感谢。

4 个答案:

答案 0 :(得分:13)

Delphi附带了一个很好的线程演示,包括GUI / Synchronize。在D2005中,它位于: C:\ Program Files \ Borland \ BDS \ 3.0 \ Demos \ DelphiWin32 \ VCLWin32 \ Threads

答案 1 :(得分:11)

我能想象的最简单的例子:(假设你在Form1上有Memo1)

procedure TForm1.Button1Click(Sender: TObject); 
begin   
  TThread.CreateAnonymousThread(procedure
      var
        slThread: TStringList;
        i: Integer;
        begin
          //do something in thread
          slThread := TStringList.Create;
          try
            for i := 0 to 100 - 1 do
            begin
              slThread.Add(Format('Value %D',[i]));
            end;

            //update gui
            TThread.Synchronize(nil, procedure
            begin
              Form1.Memo1.Lines.AddStrings(slThread);
            end);


          finally
            slThread.Free;
          end;


        end).Start;
end;

虽然我不建议使用它,因为它有一些缺点。下降你自己的TThread类更好,但对于你的问题,这个例子非常合适。

答案 2 :(得分:2)

正如已经指出的那样,创建一个更新GUI的线程并不是一个好的设计。让你的线程做一些实际工作会更好,并允许主线程更新你的显示。

虽然Delphi还提供TThread类来简化创建/管理线程,但您可能还想看看使用Delphi的BeginThread函数来执行简单的线程。有一个example on the Delphi Basics网站,我在这里转载:

// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.

unit Unit1;

interface

uses
  Forms, Dialogs, Windows, SysUtils;

type
  TMsgRecord = record
    thread : Integer;
    msg    : string[30];
  end;
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;

var
  Form1: TForm1;

Implementation
{$R *.dfm}        // Include form definitions

ThreadVar         // We must allow each thread its own instances
                  // of the passed record variable
  msgPtr : ^TMsgRecord;

// Private thread procedure to show a string
function ShowMsg(Parameter : Pointer) : Integer;
begin
  // Set up a 0 return value
  Result := 0;

  // Map the pointer to the passed data
  // Note that each thread has a separate copy of msgPtr
  msgPtr := Parameter;

  // Display this message
  ShowMessagePos('Thread '+IntToStr(msgPtr.thread)+' '+msgPtr.msg,
                 200*msgPtr.thread, 100);

  // End the thread
  EndThread(0);
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  id1, id2 : LongWord;
  thread1, thread2 : Integer;
  msg1, msg2 : TMsgRecord;

begin
  // set up our display messages
  msg1.thread := 1;
  msg1.msg    := 'Hello World';
  msg2.thread := 2;
  msg2.msg    := 'Goodbye World';

  // Start the first thread running asking for users first name
  thread1 := BeginThread(nil,
                         0,
                         Addr(ShowMsg),
                         Addr(msg1),
                         0,
                         id1);

  // And also ask for the surname
  thread2 := BeginThread(nil,
                         0,
                         Addr(ShowMsg),
                         Addr(msg2),
                         0,
                         id2);

  // Ensure that the threads are only closed when all done
  ShowMessagePos('Press this when other dialogs finished.', 200, 300);

  // Finally, tidy up by closing the threads
  CloseHandle(thread1);
  CloseHandle(thread2);
end;

end.

这里还有another example如何使用带参数的线程(使用BeginThread)。

答案 3 :(得分:2)

关于Delphi中线程的一般信息的最佳来源,没有,可以在这里找到 - http://www.eonclash.com/Tutorials/Multithreading/MartinHarvey1.1/ToC.html。去其他地方之前看看这里。