program Project1;uses
Forms,
Unit1 in 'Unit1.pas' {Form1},
Unit2 in 'Unit2.pas',
Unit3 in 'Unit3.pas' {DataModule1: TDataModule};{$R *.res}begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.CreateForm(TDataModule1, DataModule1);
Application.Run;
end.unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, ADODB;type
TForm1 = class(TForm)
ADOQuery1: TADOQuery;
Button1: TButton;
Edit1: TEdit;
ADOConnection1: TADOConnection;
Edit2: TEdit;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation
uses unit2;{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var mythread:Tmythread;
begin
Edit1.Text:=TimetoStr(Time);
Button1.Enabled:=false;
Label1.Caption:='创建新线程在运行!';
try
mythread:=Tmythread.Create(false);
except
ShowMessage('线程创建失败!');
end;
end;end.
unit Unit2;interfaceuses
Classes,unit1,unit3,
Windows, SysUtils, Variants, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, ADODB;type
Tmythread = class(TThread)
private
{ Private declarations } protected
procedure Execute; override;
procedure run;
end;implementation{ Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example, Synchronize(UpdateCaption); and UpdateCaption could look like, procedure mythread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }{ mythread }procedure Tmythread.Execute;
begin
self.run;
end;
procedure Tmythread.run;
var str:String;
begin
with Form1 do
begin
Label1.Caption:='附加线程正在执行!';// ADOConnection1.Connected:=true;
// Adoquery1.Close;
// Adoquery1.SQL.Clear;
// str:='insert into aaa'
// +'(收费编码,项目名称,项目单价,收费数量,收费金额,计费单位,计费规格,收费日期,收费对象,住院号,收费科室)'
// +' SELECT 收费编码,项目名称,项目单价,收费数量,收费金额,计费单位,计费规格,收费日期,收费对象,住院号,收费科室 FROM OpenDataSource( ''Microsoft.Jet.OLEDB.4.0'',''Data Source="F:\access_test.mdb";User ID=Admin;Password='')...住院服务明细';
// Adoquery1.Parameters.Clear;
// Adoquery1.ParamCheck := false;
// Adoquery1.SQL.Text := str;
// Adoquery1.Execsql; Edit2.Text:=TimetoStr(Time);
Application.MessageBox('执行成功!','提示');
Label1.Caption:='';
Button1.Enabled:=true;
end;end;end.unit2中注释的代码不能正常执行,提示错误为 :project project1.exe raised exception class EolException with message '尚未调用CoInitialize'。但是我不用多线程的时候能正常执行
有人能帮我解决一下吗?
Forms,
Unit1 in 'Unit1.pas' {Form1},
Unit2 in 'Unit2.pas',
Unit3 in 'Unit3.pas' {DataModule1: TDataModule};{$R *.res}begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.CreateForm(TDataModule1, DataModule1);
Application.Run;
end.unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, ADODB;type
TForm1 = class(TForm)
ADOQuery1: TADOQuery;
Button1: TButton;
Edit1: TEdit;
ADOConnection1: TADOConnection;
Edit2: TEdit;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation
uses unit2;{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var mythread:Tmythread;
begin
Edit1.Text:=TimetoStr(Time);
Button1.Enabled:=false;
Label1.Caption:='创建新线程在运行!';
try
mythread:=Tmythread.Create(false);
except
ShowMessage('线程创建失败!');
end;
end;end.
unit Unit2;interfaceuses
Classes,unit1,unit3,
Windows, SysUtils, Variants, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, ADODB;type
Tmythread = class(TThread)
private
{ Private declarations } protected
procedure Execute; override;
procedure run;
end;implementation{ Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example, Synchronize(UpdateCaption); and UpdateCaption could look like, procedure mythread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }{ mythread }procedure Tmythread.Execute;
begin
self.run;
end;
procedure Tmythread.run;
var str:String;
begin
with Form1 do
begin
Label1.Caption:='附加线程正在执行!';// ADOConnection1.Connected:=true;
// Adoquery1.Close;
// Adoquery1.SQL.Clear;
// str:='insert into aaa'
// +'(收费编码,项目名称,项目单价,收费数量,收费金额,计费单位,计费规格,收费日期,收费对象,住院号,收费科室)'
// +' SELECT 收费编码,项目名称,项目单价,收费数量,收费金额,计费单位,计费规格,收费日期,收费对象,住院号,收费科室 FROM OpenDataSource( ''Microsoft.Jet.OLEDB.4.0'',''Data Source="F:\access_test.mdb";User ID=Admin;Password='')...住院服务明细';
// Adoquery1.Parameters.Clear;
// Adoquery1.ParamCheck := false;
// Adoquery1.SQL.Text := str;
// Adoquery1.Execsql; Edit2.Text:=TimetoStr(Time);
Application.MessageBox('执行成功!','提示');
Label1.Caption:='';
Button1.Enabled:=true;
end;end;end.unit2中注释的代码不能正常执行,提示错误为 :project project1.exe raised exception class EolException with message '尚未调用CoInitialize'。但是我不用多线程的时候能正常执行
有人能帮我解决一下吗?
把它改写成Synchronize(Application.MessageBox('执行成功!','提示'))看看
如果还有错误,把下列这些语句,全部放在Synchronize()里执行,
Label1.Caption:='附加线程正在执行!';
Edit2.Text:=TimetoStr(Time);
Label1.Caption:='';
Button1.Enabled:=true;