function vfun(ir: Pointer): integer; stdcall;
var
  k, t, m, i,lk: integer;
  ss: string;
  lpwd, luserid, pass: pchar;begin
  for m := 1 to 100 do
  begin     ss := inttostr(m);// 程序发生异常,注释此句就正常,什么原因啊。
  
  end;end;function MyThreadFun(p: Pointer): Integer; stdcall;begin
  vfun(p);
  Result := 0;
end;procedure TForm1.Button1Click(Sender: TObject);
var
  ID: DWORD;
  st: array[0..9] of integer;
  ii: integer;
begin
  for ii := 0 to 9 do
  begin
    st[ii] := ii;
    hThread[ii] := CreateThread(nil, 0, @MyThreadFun, @st[ii], 0, ID);
  end;
 // Button1.Enabled := False;
end;

解决方案 »

  1.   


    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure FormDestroy(Sender: TObject);
        procedure FormCreate(Sender: TObject);
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;
      hMutex:THandle;
    implementation{$R *.dfm}
    function vfun(ir: Pointer): integer; stdcall;
    var
      k, t, m, i,lk: integer;
      ss: string;
      lpwd, luserid, pass: pchar;   
    begin
      for m := 1 to 100 do
      begin
      ss := inttostr(m);// 程序发生异常,注释此句就正常,什么原因啊。
      end;
    end;function MyThreadFun(p: Pointer): Integer; stdcall;
    begin
      vfun(p);
      Result := 0;
    end;procedure TForm1.FormDestroy(Sender: TObject);
    begin
     CloseHandle(hMutex);
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
     hMutex:=CreateMutex(nil,false,nil);
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      ID: DWORD;
      st: array[0..9] of integer;
      hThread: array[0..9] of integer;
      ii: integer;
    begin
      for ii := 0 to 9 do
      begin
        WaitForSingleObject(hMutex,INFINITE) ;
        st[ii] := ii;
        hThread[ii] := CreateThread(nil, 0, @MyThreadFun, @st[ii], 0, ID);
        ReleaseMutex(hMutex);
      end;
     // Button1.Enabled := False;
    end;
    end.
      

  2.   

    谢LSD,还是老样子啊,郁闷。
      

  3.   

    工程文件里加
    IsMultiThread := True;像这样:
    program Project1;uses
      Forms,
      Unit1 in 'Unit1.pas' {Form1};{$R *.res}begin
      IsMultiThread := True;
      Application.Initialize;
      Application.CreateForm(TForm1, Form1);
      Application.Run;
    end.
      

  4.   

    哪位好心人帮我写个示例代码吧。
    功能如下,一个按钮,两个label。使用多线程,使一个label显示1 to 1000,同时使另一个label显示1000 to 2000头疼死了,高手帮帮忙啊。
      

  5.   

    终于模仿出来了,还真麻烦。unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;
     var
     CS:TRTLCriticalSection;
    type
      TForm1 = class(TForm)
        Button1: TButton;
        Label1: TLabel;
        Label2: TLabel;
        procedure Button1Click(Sender: TObject);
        procedure FormCreate(Sender: TObject);
        procedure FormDestroy(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
     //定义线程类
    type
      tyThread = class(TThread)
      private
        Str: string;
        i1,i2:integer;
      protected    procedure showLabel;
        procedure Execute; override;
      public
        LLabel: TLabel;
      end;var
      Form1: TForm1;
      SArrayLabel: Array [0..1] of TLabel;implementation{$R *.dfm}
       //线程类实现部分procedure tyThread.Execute;
     var
     m: integer;begin
    //EnterCriticalSection(Cs); //进入临界段  for m := i1 to i2 do
      begin
      sleep(5);
      str := inttostr(m);
        synchronize(showLabel); //同步访问VCL可视组件
      end;
    //LeaveCriticalSection(Cs); //退出临界段
    end;procedure TyThread.showLabel;
    begin
    LLabel.caption:=str;
    //Application.ProcessMessages;end;procedure TForm1.Button1Click(Sender: TObject);
    var
      th:array[0..1] of  TyThread;
      ii:integer;
    begin
    //Listbox1.Clear;
    //Listbox2.Clear;
    for ii:= 0 to 1 do
    begin
      th[ii] := tyThread.Create(true); //创建线程1
      th[ii].LLabel := SArrayLabel[ii];
      th[ii].i1:=ii*1000;
      th[ii].i2:=ii*1000+1000;
      th[ii].Resume; //开始执行  end;
    end;
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      InitializeCriticalSection(Cs);
      SArrayLabel[0] := Label1;
      SArrayLabel[1] := Label2;end;procedure TForm1.FormDestroy(Sender: TObject);
    begin
       DeleteCriticalSection(Cs) ;end;end.
      

  6.   

    又不行了,线程里调用某dll里的一个函数。程序只能启动一个线程了。郁闷了。unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;
     var
     CS:TRTLCriticalSection;
    type
      TForm1 = class(TForm)
        Button1: TButton;
        Label1: TLabel;
        Label2: TLabel;
        procedure Button1Click(Sender: TObject);
        procedure FormCreate(Sender: TObject);
        procedure FormDestroy(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
     //定义线程类
    type
      tyThread = class(TThread)
      private
        Str: string;
        i1,i2:integer;
      protected    procedure showLabel;
        procedure Execute; override;
      public
        LLabel: TLabel;
      end;var
      Form1: TForm1;
      SArrayLabel: Array [0..1] of TLabel;implementation{$R *.dfm}
       //线程类实现部分procedure tyThread.Execute;
     var
     m: integer;begin
    //EnterCriticalSection(Cs); //进入临界段  for m := i1 to i2 do
      begin
      sleep(5);
      str := inttostr(m);
        synchronize(showLabel); //同步访问VCL可视组件
        
        此处调用dll函数,程序只能启动了一个线程了。
      end;
    //LeaveCriticalSection(Cs); //退出临界段
    end;procedure TyThread.showLabel;
    begin
    LLabel.caption:=str;
    //Application.ProcessMessages;end;procedure TForm1.Button1Click(Sender: TObject);
    var
      th:array[0..1] of  TyThread;
      ii:integer;
    begin
    //Listbox1.Clear;
    //Listbox2.Clear;
    for ii:= 0 to 1 do
    begin
      th[ii] := tyThread.Create(true); //创建线程1
      th[ii].LLabel := SArrayLabel[ii];
      th[ii].i1:=ii*1000;
      th[ii].i2:=ii*1000+1000;
      th[ii].Resume; //开始执行  end;
    end;
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      InitializeCriticalSection(Cs);
      SArrayLabel[0] := Label1;
      SArrayLabel[1] := Label2;end;procedure TForm1.FormDestroy(Sender: TObject);
    begin
       DeleteCriticalSection(Cs) ;end;end.