我用了如下的代码
unit FibMain;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, AppEvnts;const
DDG_THREADMSG = WM_USER;
type
TForm1 = class(TForm)
BtnWee: TButton;
BtnStop: TButton;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
AppEvents: TApplicationEvents;
procedure BtnWeeClick(Sender: TObject);
procedure BtnStopClick(Sender: TObject);
procedure AppEventsMessage(var Msg: tagMSG; var Handled: Boolean);
private
{ Private declarations }
FThreadID: LongWord;
FThreadHandle: Integer;
procedure WMMYMESSAGE(var Msg:TMessage);message DDG_THREADMSG;
public
{ Public declarations }
end;var
Form1: TForm1;implementationuses Fibers;{$R *.dfm}var
FFibers: array[0..3] of Pointer;
StopIt: Boolean;procedure FiberFunc(Param: Pointer); stdcall;
var
J, FibNum, NextNum: Integer;
I: Cardinal;
Fiber: Pointer;
begin
try
I := 0;
FibNum := 1; // supress compiler warning
Fiber := GetCurrentFiber; // save away our fiber ptr for later
// figure out where current fiber is in the array and save for later
for J := Low(FFibers) to High(FFibers) do
if FFibers[J] = Fiber then
begin
FibNum := J;
Break;
end;
// HIGH TECH: count from zero to really, really high
while not StopIt do
begin
// send the number to the main thread for display every 100
if I mod 100 = 0 then
PostMessage(Application.Handle, DDG_THREADMSG, Integer(GetFiberData), I);
// switch fibers every 1000
if I mod 1000 = 0 then
begin
if FibNum = High(FFibers) then NextNum := Low(FFibers)
else NextNum := FibNum + 1;
SwitchToFiber(FFibers[NextNum]);
end;
Inc(I);
end;
except
// stifle all unhandled exceptions
end;
end;function ThreadFunc(Param: Pointer): Integer;
var
I: Integer;
begin
Result := 0;
// convert this thread to a fiber
FFibers[0] := Pointer(ConvertThreadToFiber(Pointer(1)));
// create the other fibers
FFibers[1] := Pointer(CreateFiber(0, @FiberFunc, Pointer(2)));
FFibers[2] := Pointer(CreateFiber(0, @FiberFunc, Pointer(3)));
FFibers[3] := Pointer(CreateFiber(0, @FiberFunc, Pointer(4)));
// join in the fun
FiberFunc(Pointer(1));
// when done, kill all the fibers
// killing the current fiber calls ExitThread
for I := High(FFibers) downto Low(FFibers) do
DeleteFiber(FFibers[I]);
end;procedure TForm1.BtnWeeClick(Sender: TObject);
begin
BtnWee.Enabled := False; // pressing the button twice will cause grief
FThreadHandle := BeginThread(nil, 0, @ThreadFunc, nil, 0, FThreadID);
end;procedure TForm1.BtnStopClick(Sender: TObject);
begin
StopIt := True;
end;procedure TForm1.AppEventsMessage(var Msg: tagMSG; var Handled: Boolean);
begin
{if Msg.message = DDG_THREADMSG then
begin
// The wParam tells us which fiber is sending the message,
// and therefore which label to update
case Msg.wParam of
1: Label1.Caption := IntToStr(Msg.lParam);
2: Label2.Caption := IntToStr(Msg.lParam);
3: Label3.Caption := IntToStr(Msg.lParam);
4: Label4.Caption := IntToStr(Msg.lParam);
end;
Handled := True;
end;}
end;procedure TForm1.WMMYMESSAGE(var Msg: TMessage);
begin
case Msg.wParam of
1: Label1.Caption := IntToStr(Msg.lParam);
2: Label2.Caption := IntToStr(Msg.lParam);
3: Label3.Caption := IntToStr(Msg.lParam);
4: Label4.Caption := IntToStr(Msg.lParam);
end;
inherited;end;end.以上是我的程序一段代码作用是使用纤程发送消息给主窗口 窗口通过TApplicationEvents
来显示结果 但现在我自己写了个消息处理的函数 (同时除去了TApplicationEvents里面的
处理) 但结果就没有显示case Msg.wParam of
1: Label1.Caption := IntToStr(Msg.lParam);
2: Label2.Caption := IntToStr(Msg.lParam);
3: Label3.Caption := IntToStr(Msg.lParam);
4: Label4.Caption := IntToStr(Msg.lParam);
end;请问我哪写的不对??
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货