創建線程對象時,我想從需要同步的應用程序調用代碼。問題是我不知道如何使用參數調用Synchronize
來應用函數。將參數傳遞給同步過程調用
說我們有
procedure ThreadObject.Execute;
var
val1,val2:integer;
Star:string;
begin
Synchronize(funcyfunc); //how to pass val1,val2,star here?
end;
其中funcyfunc
被定義爲跟隨
procedure OtherClass.funcyfunc(param1,param2:integer;spok:string);
begin
letsCallFriends(spok,param1);
letsCallFriends(spok,param2);
end;
現在奇的解決方案,這將是在ThreadObject私下裏說
,並在我們的實施
procedure ThreadObject.starVal;
begin
funcyfunc(Star,val1,val2);
end;
,並在線程中執行,我們改變
procedure ThreadObject.Execute;
var
val1,val2:integer;
Star:string;
begin
Synchronize(starVal); //how to pass val1,val2,star here?
end;
但這是一件麻煩事,因爲每個過程調用必須有一個全局變量。有沒有更好的辦法?
代碼:
unit ThreadObject;
interface
uses
Classes;
type
TThreadObject= class(TThread)
private
star:string;
val1,val2:integer;
procedure starVal;
protected
procedure Execute; override;
//assume we have a constructor that ini the star val1 val2.
end;
implementation
{ TThreadObject }
procedure ThreadObject.Execute;
var
val1,val2:integer;
Star:string;
begin
Synchronize(starVal); //how to pass val1,val2,star here?
end;
procedure ThreadObject.starVal;
begin
funcyfunc(Star,val1,val2);
end;
end.
['This way way](http://pastebin.com/WD2LYNE5)。 – TLama
有一個很好的博客文章處理這個http://www.uweraabe.de/Blog/2011/01/30/synchronize-and-queue-with-parameters/ –
可能重複的[傳遞值同步線程]( http://stackoverflow.com/questions/16870387/passing-value-to-synchronize-thread) – CinCout