Thread1[i].OnTerminate編譯錯誤:[DCC Error] MainWin.pas(48): E2010 Incompatible types |
答題得分者是:jow
|
ctimesys
一般會員 發表:1 回覆:0 積分:0 註冊:2007-11-04 發送簡訊給我 |
下列程式執行時,會出現[DCC Error] MainWin.pas(48): E2010 Incompatible types: 'TNotifyEvent' and 'Integer'錯誤,請問是哪邊出了問題,感恩.
unit MainWin; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Menus, StdCtrls , Tabs, ExtCtrls, ComCtrls, IdHTTP,GetWeb_Thread_Fnc; type TForm1 = class(TForm) MainWinMenu: TMainMenu; N1: TMenuItem; GetButton: TButton; ShowState: TLabel; URLList: TMemo; OutPutArae: TMemo; tmparea: TMemo; Shape1: TShape; Shape2: TShape; procedure GetButtonClick(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; Thread1: array of T1; i:integer; sendURL: string; function GetData_ThreadExit(theid: integer):integer; implementation {$R *.dfm} procedure TForm1.GetButtonClick(Sender: TObject); begin SetLength(Thread1,10); sendURL:='www.abc.com'; for i := 0 to 9 do begin Thread1[i] := T1.Create(URLList.Lines.Strings[i],i); Thread1[i].OnTerminate := GetData_ThreadExit(i); //此行會出現錯誤 end; end; function GetData_ThreadExit(theid: integer):integer; begin end; end. |
jow
尊榮會員 發表:66 回覆:751 積分:1253 註冊:2002-03-13 發送簡訊給我 |
dcc 明白指出了型別不相容的訊息....
問題點在於掛接到TThread的OnTerminate 的事件處理程序 (Event Handler)型態不符, 先看看 TThread.OnTerminate的定義: property OnTerminate: TNotifyEvent; 再看看TNotifyEvent 的定義: type TNotifyEvent = procedure (Sender: TObject) of object; 所以你的 function GetData_ThreadExit(theid: integer):integer; 1. 型別錯誤, 它應該是一個 TNotifyEvent; 2.function GetData_ThreadExit() 應該宣告在class以內. 3.掛接Event Handler 時, 只要指定其函數名稱,它其實是將函式的 實作程式碼位址指定給一個class中特定事件型別的一個Memory Member. 以下是程式上的建議: (1)另設Event Handler TForm1 = class(TForm) (略) private procedure Do_OnTerminate(Sender: TObject); public { Public declarations } end; implementation procedure TForm1.GetButtonClick(Sender: TObject); begin SetLength(Thread1,10); sendURL:='www.abc.com'; for i := 0 to 9 do begin Thread1[i] := T1.Create(URLList.Lines.Strings[i],i); Thread1[i].OnTerminate := Do_OnTerminate; end; end; procedure TForm1.Do_OnTerminate(Sender: TObject); var T: T1; ReturnValue: Integer; begin if Sender is T1 then begin T := Sender as T1; ReturnValue := GetData_ThreadExit(T.ID):// ID是 T1中定義的屬性 end; end; (2)更改GetData_ThreadExit()的型態為TNotifyEvent, 並將宣告移到 TForm1 class內. Event Handler 指定方式: procedure TForm1.GetButtonClick(Sender: TObject); begin SetLength(Thread1,10); sendURL:='www.abc.com'; for i := 0 to 9 do begin Thread1[i] := T1.Create(URLList.Lines.Strings[i],i); Thread1[i].OnTerminate := GetData_ThreadExit; end; end; 以上建議, 僅供參考 ............^_^
編輯記錄
jow 重新編輯於 2007-11-05 10:33:00, 註解 無‧
|
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |