关于多线程的问题,不知为何出错?请各位大侠帮忙看看?(30分)

  • 主题发起人 主题发起人 jackyzjh
  • 开始时间 开始时间
J

jackyzjh

Unregistered / Unconfirmed
GUEST, unregistred user!
unit OrderJinDu;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Base, dxCntner, dxExEdtr, StdCtrls, Mask, DBCtrls, dxDBTLCl,
dxGrClms, dxTL, dxDBCtrl, dxDBGrid, ExtCtrls, DB, ADODB, RzPanel, RzSplit,
ComCtrls, dxtree, ImgList,PubMessage, dxEditor, dxEdLib, dxDBELib;
type
TMyThread=class(TThread)
private
protected
procedure Execute;
override;
end;
FieldArray=array of TField;
TfrmOrderJinDu = class(Tfrm)
odr_indents: TADODataSet;
odr_indentsindent_no: TStringField;
odr_indentsmat_no: TStringField;
odr_indentsmat_nm: TStringField;
odr_indentscus_matno: TStringField;
odr_indentsweight: TBCDField;
odr_indentsrongqi: TStringField;
odr_indentsquan: TIntegerField;
odr_indentsqty: TBCDField;
odr_indentsprice: TBCDField;
odr_indentsamt: TBCDField;
odr_indentsmark: TStringField;
dsodr_indents: TDataSource;
odr_deliverys: TADODataSet;
dsodr_deliverys: TDataSource;
odr_deliverysindent_no: TStringField;
odr_deliverysvou_no: TStringField;
odr_deliverysmat_no: TStringField;
odr_deliveryscus_matno: TStringField;
odr_deliverysweight: TBCDField;
odr_deliverysquan: TIntegerField;
odr_deliverysqty: TBCDField;
odr_deliverysprice: TBCDField;
odr_deliverysamt: TBCDField;
Panel1: TPanel;
Panel2: TPanel;
p_matm: TADODataSet;
p_matmmat_no: TStringField;
p_matmmat_nm: TStringField;
dsp_matm: TDataSource;
odr_deliveryscus_no: TStringField;
odr_deliveryssales: TStringField;
odr_deliverysddate: TDateTimeField;
odr_deliverysmat_nm: TStringField;
odr_deliverysnRcdNo: TSmallintField;
odr_indent: TADODataSet;
odr_indentindent_no: TStringField;
odr_indentcus_no: TStringField;
odr_indentsales: TStringField;
odr_indentdinghuoren: TStringField;
odr_indentcus_pono: TStringField;
odr_indentind: TDateTimeField;
odr_indentetd: TDateTimeField;
odr_indentistax: TBooleanField;
odr_indenttax_rate: TBCDField;
odr_indentpayno: TStringField;
odr_indentdelimode: TStringField;
odr_indentdeliaddr: TStringField;
odr_indentmark: TMemoField;
odr_indentcus_par: TStringField;
dsodr_indent: TDataSource;
odr_indentscus_no: TStringField;
odr_indentssales: TStringField;
RzSizePanel1: TRzSizePanel;
dxTreeView1: TdxTreeView;
ImageList1: TImageList;
odr_deliverysetd: TDateField;
odr_deliverysywdate: TIntegerField;
dxDBGrid1: TdxDBGrid;
dxDBGrid1indent_no: TdxDBGridMaskColumn;
dxDBGrid1vou_no: TdxDBGridMaskColumn;
dxDBGrid1ddate: TdxDBGridDateColumn;
dxDBGrid1etd: TdxDBGridColumn;
dxDBGrid1ywdate: TdxDBGridColumn;
dxDBGrid1mat_no: TdxDBGridMaskColumn;
dxDBGrid1mat_nm: TdxDBGridLookupColumn;
dxDBGrid1cus_matno: TdxDBGridMaskColumn;
dxDBGrid1weight: TdxDBGridColumn;
dxDBGrid1quan: TdxDBGridMaskColumn;
dxDBGrid1qty: TdxDBGridColumn;
dxDBGrid1price: TdxDBGridCurrencyColumn;
dxDBGrid1amt: TdxDBGridCurrencyColumn;
dxDBGrid1cus_no: TdxDBGridMaskColumn;
dxDBGrid1sales: TdxDBGridMaskColumn;
p_customer: TADODataSet;
p_customercus_no: TStringField;
p_customercus_par: TStringField;
p_customersales: TStringField;
dsp_customer: TDataSource;
temp: TADODataSet;
Panel3: TPanel;
temp1: TADODataSet;
Label1: TLabel;
DBEdit1: TdxDBEdit;
Label2: TLabel;
DBLookupComboBox1: TdxDBEdit;
Label4: TLabel;
DBLookupComboBox3: TdxDBEdit;
Label9: TLabel;
Label3: TLabel;
Label7: TLabel;
Label10: TLabel;
Label11: TLabel;
Label12: TLabel;
DBEdit4: TdxDBEdit;
DBEdit5: TdxDBEdit;
DBEdit6: TdxDBEdit;
DBEdit7: TdxDBEdit;
DBEdit8: TdxDBEdit;
DBEdit9: TdxDBEdit;
Label6: TLabel;
Label8: TLabel;
DBEdit2: TdxDBEdit;
DBLookupComboBox5: TdxDBEdit;
DBEdit3: TdxDBEdit;
Label5: TLabel;
DBLookupComboBox4: TdxDBDateEdit;
odr_indentsetd: TDateField;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
frmOrderJinDu: TfrmOrderJinDu;
implementation
uses MainFrm_pas;
{$R *.dfm}

{ TMyThread }

procedure TMyThread.Execute;
begin
frmOrderJinDu.dxTreeView1.Items.AddChild(nil,'所有订单');//这里出错???????
end;

procedure TfrmOrderJinDu.FormCreate(Sender: TObject);
var
r:real;
mythread:TMyThread;
begin
inherited;
r:=gettickcount;
//BuildTree;
mythread:=TMyThread.Create(false);
//mythread.Execute;
//mythread.Resume;
//showmessage(floattostr((gettickcount-r)/1000));
end;

initialization
registerclass(TfrmOrderJinDu);
finalization
unregisterclass(TfrmOrderJinDu);
end.
 
线程中访问vcl控件要放在Synchronize中同步
 
Synchronize,是的要与主线程同步,而且要注意线程安全问题。对于VCL,有些是线程安全
有些不是线程安全的。
 
to delphiboy
如何分辩线程是否安全?
 
TMyThread=class(TThread)
private
protected
procedure Execute;
override;
[red]procedure Add;[/red]
end;

procedure TMyThread.Execute;
begin
[red]Synchronize(Add);
//同步到主线程[/red]
end;

procedure TMyThread.Add;
begin
frmOrderJinDu.dxTreeView1.Items.AddChild(nil,'所有订单');
[red]//因为你用了VCL与界面打交道的操作,不能在线程中进行,要同步到主线程中[/red] 
end;
 
后退
顶部