以文本方式查看主题

-  W3CHINA.ORG讨论区 - 语义网·描述逻辑·本体·RDF·OWL  (http://bbs.xml.org.cn/index.asp)
--  『 编程心得 』   (http://bbs.xml.org.cn/list.asp?boardid=42)
----  这段程序有问题吗  (http://bbs.xml.org.cn/dispbbs.asp?boardid=42&rootid=&id=36012)


--  作者:banono
--  发布时间:7/25/2006 11:12:00 AM

--  这段程序有问题吗
这段程序有问题吗为什么不能运行
unit Unit1;

interface

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

type
  TTBForm = class(TForm)
    ListBox1: TListBox;
    Button1: TButton;
    procedure button1click(sender:tobject);
  private
    { Private declarations }
    procedure threadsdone(sender:tobject);
  public
    { Public declarations }
  end;
  tlnithread=class(tthread)
  protected
    procedure execute;override;
  end;

var
  TBForm: TTBForm;

implementation

{$R *.dfm}
const
  maxsize=128;
var
  nextnum:integer=0;
  doneflags:integer=0;
  globalarray:array[1..maxsize] of integer;

function getnextnum:integer;
begin
  result:=nextnum;
  inc(nextnum);
end;

procedure tlnithread.execute;
var
  i:integer;
begin
  onterminate:=TBForm.threadsdone;
  for i:=1 to maxsize do
  begin
    globalarray[i]:=getnextnum;
    sleep(5);
  end;
end;

procedure TTBForm.button1click(sender:tobject);
begin
  tlnithread.Create(false);
  tlnithread.Create(false);
end;

procedure ttbform.threadsdone(sender:tobject);
var
  i:integer;
begin
  inc(doneflags);
  if doneflags=2 then
    for i:=1 to maxsize do
      listbox1.Items.Add(inttostr(globalarray[i]));
end;

end.


--  作者:banono
--  发布时间:7/26/2006 8:50:00 AM

--  
下面是用临界区解决线程同步的问题,怎么也不能运行?555555,求助高手~~~~~~~~~~~~~~~~~
unit Unit1;

interface

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

type
  TTBForm = class(TForm)
    ListBox1: TListBox;
    Button1: TButton;
    procedure Button1Click(Sender:Tobject);
  private
    { Private declarations }
    procedure ThreadsDone(Sender:Tobject);
  public
    { Public declarations }
  end;
  TIniThread=class(TThread)
  protected
    procedure Execute;override;
  end;

var
  TBForm: TTBForm;

implementation

{$R *.dfm}
const
  MaxSize=128;
var
  NextNum:integer=0;
  DoneFlags:integer=0;
  GlobalArray:array[1..MaxSize] of integer;
  CS:TRTLCriticalSection;

function GetNextNum:integer;
begin
  Result:=NextNum;
  Inc(NextNum);
end;

procedure TIniThread.execute;
var
  i:integer;
begin
  OnTerminate:=TBForm.ThreadsDone;
  EnterCriticalSection(CS);
  for i:=1 to MaxSize do
  begin
    GlobalArray[i]:=GetNextNum;
    Sleep(5);
  end;
  LeaveCriticalSection(CS);
end;

procedure TTBForm.Button1Click(Sender:Tobject);
begin
  InitializeCriticalSection(CS);
  TIniThread.Create(false);
  TIniThread.Create(false);
end;

procedure TTBform.ThreadsDone(Sender:Tobject);
var
  i:integer;
begin
  Inc(DoneFlags);
  if DoneFlags=2 then
    for i:=1 to MaxSize do
      ListBox1.Items.Add(IntToStr(GlobalArray[i]));
end;

end.


W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
23.438ms