读书人

请问一个有关进程的解决方案

发布时间: 2012-03-02 14:40:28 作者: rapoo

请教一个有关进程的解决方案
我做了个处理Excel文件的程序,由于有时候数据量很大,要花费较长的时间,我想在处理excel的同时,弹出一个窗口来显示处理进度,不至于用户以为死机了,我用全局变量来传递处理进度,在form1中用了线程来容纳处理excel的函数并修改公用变量,在form2中用计时器来得到全局变量的值。
form1代码:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls,comobj;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
type
TtestThread = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
end;
var
Form1: TForm1;

implementation

uses Unit2, Unit3;

{$R *.dfm}

procedure TtestThread.Execute;
var
i:integer;
ExcelApp:variant;
begin
ExcelApp := CreateOleObject( 'Excel.Application ' );
ExcelApp.WorkBooks.Add;
for i:=1 to 1000 do
begin
ExcelApp.Cells[i,1].Value := '第一行第四列 ';
p:=i;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
ExcelApp: Variant;
testThread:TtestThread;
begin
testThread:=TtestThread.Create(false);
form2.ShowModal;
end;

end.
///////////////////////////
form2代码:
unit Unit2;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, ComCtrls;

type
TForm2 = class(TForm)
ProgressBar1: TProgressBar;
Timer1: TTimer;
procedure Timer1Timer(Sender: TObject);
procedure FormShow(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form2: TForm2;

implementation

uses Unit3, Unit1;

{$R *.dfm}

procedure TForm2.Timer1Timer(Sender: TObject);
begin
progressbar1.Position:=p;
if progressbar1.Position=1000 then
begin
Timer1.Enabled:=false;
form1.Enabled:=True;
end;
end;

procedure TForm2.FormShow(Sender: TObject);
begin
timer1.Enabled:=True;
end;

end.


/////////////////////
form3代码:
unit Unit3;

interface
var
p:integer;
implementation

end.

运行时,发现错误:尚未调用coinitialize。各位给看看错在哪里,提出个解决方案看看。谢谢


[解决办法]
回复:

initialization
Coinitialize(nil);
finalization
CoUninitialize;

[解决办法]

procedure TtestThread.Execute;
var
i:integer;
ExcelApp:variant;
begin
coinitialize

ExcelApp := CreateOleObject( 'Excel.Application ' );
ExcelApp.WorkBooks.Add;
for i:=1 to 1000 do
begin
ExcelApp.Cells[i,1].Value := '第一行第四列 ';
p:=i;
end;
end;


读书人网 >.NET

热点推荐