多线程如何和窗体实现互动???(100分)

小邱

Unregistered / Unconfirmed
GUEST, unregistred user!
unit U_TelRegThreed;/////////线程//////
interface
uses
Classes,ExtCtrls,Dialogs,SysUtils,U_TelReg,U_OnLine,Forms;
type
TConnect = class(TThread)
private
{ Private declarations }
fHosId,fDocId,fUserId:string;
fBalance,fRate:double;
i:integer;
fTime:smallint;
MeetingId:smallint;//////////会议号//////////
procedure Timers;
protected
procedure Execute;
override;
public
aForm: TFTelReg;
constructor Create(Suspended:Boolean;HosId,DocId,UserId:string;Balance:double;Time:smallint;Rate:double);
destructor destroy;override;
end;

implementation
uses U_Main,U_Users;
procedure TConnect.Timers;
var
h,m,s:integer;
begin
if not Assigned(aForm) then
//这样保证FTelReg在FREE之后不再执行
Exit;
i:=i+1;
if (i mod 60=0) then
aForm.SB.Panels[0].Text :='预计:'+floattostr(fTime-1)+'分钟';
if i<60 then
aForm.SB.Panels[2].Text :='计时:'+floattostr(i)+'秒'
else
if (i>=60)and (i<3600) then
begin
m:=i div 60;
s:=i mod 60;
aForm.SB.Panels[2].Text :='计时:'+floattostr(m)+'分'+floattostr(s)+'秒';
end
else
begin
h:=i div 3600;
m:=i div 60;
s:=i mod 3600;
aForm.SB.Panels[2].Text :='计时:'+floattostr(h)+'时'+floattostr(m)+'分'+floattostr(s)+'秒';
end;
aForm.SB.Panels[3].Text :='状态:通话中……';
end;
constructor TConnect.Create(Suspended:Boolean;HosId,DocId,UserId:string;Balance:double;Time:smallint;Rate:double);
begin
inherited Create(Suspended);
fHosId:=HosId;
fDocId:=DocId;
fUserId:=UserId;
fBalance:=Balance;
fTime:=Time;
fRate:=Rate;
FreeOnTerminate:=True;
aForm := TFTelReg.Create(Application);
aForm.Show;
aForm.SB.Panels[0].Text :='预计:'+floattostr(fTime)+'分钟';
MeetingId:=MainForm.HxdAgent.begin
Charge(fRate,fTime);/////(问题1)////////这里触发OCX方法,每建立一个会议,得到一个会议号MeetingId
aForm.SetMeetingId(MeetingId,fUserId,Time,CliId,fHosId+fDocId,Caller);
end;
procedure TConnect.Execute;
begin
i:=0;
aForm.SB.Panels[1].Text :='开始时间:'+Timetostr(Time);
while not Terminateddo
begin
Sleep(1000);
Synchronize(Timers);//同步调用
end;
end;
destructor TConnect.destroy ;
begin
if Assigned(aForm) then
FreeAndNil(aForm);
inherited;
end;
end.
////////////////////以下是模板窗体的部分代码/////////////////////
unit U_TelReg;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, TEMPLETFORM, ComCtrls, Menus, ToolWin, StdCtrls, ExtCtrls,
Buttons;
type
TFTelReg = class(TMyForm)
SB: TStatusBar;
Timer1: TTimer;
ListBox1: TListBox;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
BitBtn3: TBitBtn;
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
procedure BitBtn1Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
procedure BitBtn3Click(Sender: TObject);
private
{ Private declarations }
MeetingId:smallint=-1;//////////////会议号,一个模板窗体实例对应一个会议号//////////
m_UserId:string;
m_begin
,m_End:Tdatetime;
m_Zx_id:byte;
m_Ysm:string;
m_InTel:string;
public
{ Public declarations }
procedure SetMeetingId(id:smallint;m_U:string;m_B:TDatetime;m_Zx:byte;m_Y,m_Tel:string);
///////////该过程主要是提供给线程设置将要生成的模板窗体实例的会议号
end;

var
FTelReg: TFTelReg;
implementation
uses U_TelRegThreed, U_OnLine,U_Main;
{$R *.dfm}
procedure TFTelReg.SetMeetingId(id:smallint;m_U:string;m_B:TDatetime;m_Zx:byte;m_Y,m_Tel:string);
begin
MeetingId:=id;
m_UserId:=m_U;
m_begin
:=m_B;
m_Zx_id:=m_Zx;
m_Ysm:=m_Y;
m_InTel:=m_Tel;
end;
问题(1):我在模板定义了一个私有变量MeetingiD,来对应每个窗体实例。
这时,OCX触发了一个事件,传递过来一个会议号(再编一个线程,等待中……),假设为2,要求结束会议号为2的会议,
假设这是已有4个会议(会议分别为1,2,3,4)【问:】如何实现对会议号为2的窗体实例
的操作,比如使该窗体实例显示在最前面,并Showmessage('该会议结束‘),然后根据该窗体
对应的数据进行写流水之类的操作??
问题2:线程U_TelRegThread中可以维护这样的一个动态数组么?分别对应:会议号1:aform1;会议号2:aform2……
其中aformX为窗体实例的FORM.NAME;得到一个会议号,找该会议号对应的窗体实例,实现对该
窗体的操作?
问题3:窗体实例写流水的方法也调用一个线程,该窗体的数据如何赋给这个线程。
呵呵,看起来很复杂(主要是我所说的不知清不清楚),我对多线程不熟,很急,谢谢了。
 
没空详细看你的程序,我想DFW上贴一大段代码然后问题的方式不是很好,因为大家都不是专业来
这里回答问题的,大多没有时间和耐心看你一大段程序。建议你不懂的问题提出重点,我们可以
给你观点和建议,最后还需要你自己领会和有选择地吸收。
 
呵呵,其实这段代码非常简单,根据我的问题,稍微看一下就可以知道
原委及如何处理了。
 
一般的可以吧,我用线程写到form中的progressbar.panels.items[0].text的,可以啊
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
679
import
I
顶部