R
robotin
Unregistered / Unconfirmed
GUEST, unregistred user!
我想做一个线程,它的任务就是调用excel。
如下所示:
//线程
unit Unit2;
interface
uses
Classes,Windows;
type
TMyThread = class(TThread)
private
{ Private declarations }
protected
procedure Execute;
override;
end;
var
hSemaphore: THandle;
implementation
uses comObj,SysUtils;
{ TMyThread }
procedure TMyThread.Execute;
begin
WaitForSingleobject (hSemaphore, 100000);
ExcelApplication:=CreateOleObject('Excel.Application');
ExcelApplication.Visible:=true;
ExcelApplication.workbooks.add(1);
ExcelWorkSheet:=ExcelApplication.workbooks[1].sheets[1];
ReleaseSemaphore(hSemaphore, 1, nil);
end;
initialization
hSemaphore:=CreateSemaphore(
nil, 10, 10, 'ThDB_MD_Semaphore');
end.
//调用
……
procedure TForm1.BitBtn1Click(Sender: TObject);
var GetDataThread:TGetDataThread;
begin
GetDataThread:= TGetDataThread.Create (True);
GetDataThread.Priority := tpLowest;
GetDataThread.FreeOnTerminate:=True;
GetDataThread.Resume;
end;
……
这样做好象不行。
如下所示:
//线程
unit Unit2;
interface
uses
Classes,Windows;
type
TMyThread = class(TThread)
private
{ Private declarations }
protected
procedure Execute;
override;
end;
var
hSemaphore: THandle;
implementation
uses comObj,SysUtils;
{ TMyThread }
procedure TMyThread.Execute;
begin
WaitForSingleobject (hSemaphore, 100000);
ExcelApplication:=CreateOleObject('Excel.Application');
ExcelApplication.Visible:=true;
ExcelApplication.workbooks.add(1);
ExcelWorkSheet:=ExcelApplication.workbooks[1].sheets[1];
ReleaseSemaphore(hSemaphore, 1, nil);
end;
initialization
hSemaphore:=CreateSemaphore(
nil, 10, 10, 'ThDB_MD_Semaphore');
end.
//调用
……
procedure TForm1.BitBtn1Click(Sender: TObject);
var GetDataThread:TGetDataThread;
begin
GetDataThread:= TGetDataThread.Create (True);
GetDataThread.Priority := tpLowest;
GetDataThread.FreeOnTerminate:=True;
GetDataThread.Resume;
end;
……
这样做好象不行。