如何在delphi中直接在线程外部终止线程?

时间:2014-07-26 20:04:14

标签: multithreading delphi kill

我想点击按钮终止线程。如果线程在没有用户中断的情况下正常工作就可以了,但有时用户需要中止线程,这就是用户如何中止线程的问题。

这是我测试的代码:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, mmsystem, ExtCtrls;

type
  TForm1 = class(TForm)
    Image1: TImage;
    procedure Image1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  end;

type
  hangth = class(TThread)
  private
    { Private declarations }
  protected
    procedure Execute; override;
    procedure play;
  end;

var
  Form1: TForm1;
  played: boolean;
  szalhang: hangth;

implementation

{$R *.dfm}

procedure hangth.play;
begin
  played := true;
  szalhang.Terminate;
end;

procedure hangth.Execute;
begin
  played := false;
  SndPlaySound(pchar('hang.wav'), SND_SYNC);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  played := true;
end;

procedure TForm1.Image1Click(Sender: TObject);
begin
  if played = true then begin
    szalhang := hangth.Create(true);
    szalhang.Resume;
  end else begin
    szalhang.Terminate();
    // here i want to terminate thread, but it doesn't want to be killed.
  end;
end;

end.

1 个答案:

答案 0 :(得分:0)

当您致电TThread.Terminate()时,它会将TThread.Terminated属性设置为true,不执行任何其他操作。您的TThread.Execute()代码有责任定期查看TThread.Terminated属性,并在其为True时正常退出。但是,在这种情况下,这是不可能的,因为SndPlaySound()阻塞了线程,并且在SndPlaySound()模式下运行时无法中断SND_SYNC。您唯一的选择是使用Win32 API TerminateThread()函数来执行线程的暴力终止。

由于您显然需要更多地控制音频的播放,并检测音频播放的时间,因此SndPlaySound()不是满足您需求的最佳解决方案。您有一个TForm,您可以考虑使用Delphi的TMediaPlayer组件,例如:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.MPlayer;

type
  MPlayerState = (mpsClosed, mpsOpened, mpsPlaying);

  TForm1 = class(TForm)
    Image1: TImage;
    MediaPlayer1: TMediaPlayer;
    procedure MediaPlayer1Notify(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Image1Click(Sender: TObject);
  private
    { Private declarations }
    State: MPlayerState;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm56.FormCreate(Sender: TObject);
begin
  State := mpsClosed;
  MediaPlayer1.FileName := 'C:\full path to\hang.wav';
end;

procedure TForm56.MediaPlayer1Notify(Sender: TObject);
begin
  case MediaPlayer1.Mode of
    mpStopped, mpPlaying:
      State := mpsOpened;
  end;
end;

procedure TForm1.Image1Click(Sender: TObject);
begin
  if State = mpsClosed then
  begin
    MediaPlayer1.Notify := False;
    MediaPlayer1.Wait := True;
    MediaPlayer1.Open;
    State := mpsOpened;
  end;

  if State = mpsOpened then
  begin
    MediaPlayer1.Notify := True;
    MediaPlayer1.Wait := False;
    MediaPlayer1.Play;
    if MediaPlayer1.Error = 0 then
      State := mpsPlaying
  end else
  begin
    MediaPlayer1.Notify := False;
    MediaPlayer1.Wait := True;
    MediaPlayer1.Stop;
    State := mpsOpened;

    MediaPlayer1.Notify := False;
    MediaPlayer1.Wait := True;
    MediaPlayer1.Close;
    State := mpsClosed;
  end;
end;

end.