Board index » delphi » Access Violation terminating thread containing OLEvar

Access Violation terminating thread containing OLEvar

I have a background thread (TThread class) which writes processed data
to an Excel workbook.

I have structured the execute method as follows....

..
procedure TXLReportThread.Execute;
begin
  repeat
    While RunIt do
    begin
      OLEInitialize(nil);
      XLServer:=CreateOLEObject('Excel.application');
      XLServer.DisplayAlerts:=false;
      XLServer.visible:=false;
      XPRC:=CurrentPRC^;
      ReportToExcel;
      XLServer.quit;
      OLEUninitialize;
      RunIt:=false;
    end;
    if terminated then Closeit:=true;
  until Closeit;
end;

...I have set the FreeOnTerminate to TRUE during creation
...No override in the DoTerminate event
...Critical sections are around all areas which access Non-local
variables and objects and  no confilcts occur in those areas.

The thread is created at application startup and suspended until I
resume it and call the RUN method which sets RunIt to true and allows
the execution to begin...

The execute method works fine and the Excel object is released properly
when I call its QUIT method.. Also subsequent resumes and suspend work
fine...

I terminate the thread by calling its CLOSE method which sets CloseIt to
true, releases the Excel Object and procedes with termination... But I
get an access violation when it terminates.  Once I hit OK at the
violation message, the application then exists normally....

I have tried everything I can think of to correct this but to no
avail...Does anyone have any thoughts?

Thanks,

Tom Grills  tgri...@envantage.com
Information Services

Envantage Analytical Software        
15585 NEO Parkway
Cleveland, OH 44128

 

Re:Access Violation terminating thread containing OLEvar


Hello,

Try assigning XLServer := Unassigned before calling OleUninitialize. If this
still doesn't work, try to do early binding to Excel.Application, i.e.,
don't use XLServer as a variant and then assign it to NIL before calling
OleUninitialize.

I have a feeling that OleUninitialize is being called too early, because
Delphi garbage-collects variants only at the end of the procedure and
obviously, you can't call OleUninitialize until then.

have fun,

--
Binh Ly
Brickhouse Data Systems, Inc.
http://www.brickhouse.com

Quote
Tom Grills wrote in message <35AF5BBC.A3A5F...@ntr.net>...
>I have a background thread (TThread class) which writes processed data
>to an Excel workbook.

>I have structured the execute method as follows....

>..
>procedure TXLReportThread.Execute;
>begin
>  repeat
>    While RunIt do
>    begin
>      OLEInitialize(nil);
>      XLServer:=CreateOLEObject('Excel.application');
>      XLServer.DisplayAlerts:=false;
>      XLServer.visible:=false;
>      XPRC:=CurrentPRC^;
>      ReportToExcel;
>      XLServer.quit;
>      OLEUninitialize;
>      RunIt:=false;
>    end;
>    if terminated then Closeit:=true;
>  until Closeit;
>end;

>...I have set the FreeOnTerminate to TRUE during creation
>...No override in the DoTerminate event
>...Critical sections are around all areas which access Non-local
>variables and objects and  no confilcts occur in those areas.

>The thread is created at application startup and suspended until I
>resume it and call the RUN method which sets RunIt to true and allows
>the execution to begin...

>The execute method works fine and the Excel object is released properly
>when I call its QUIT method.. Also subsequent resumes and suspend work
>fine...

>I terminate the thread by calling its CLOSE method which sets CloseIt to
>true, releases the Excel Object and procedes with termination... But I
>get an access violation when it terminates.  Once I hit OK at the
>violation message, the application then exists normally....

>I have tried everything I can think of to correct this but to no
>avail...Does anyone have any thoughts?

>Thanks,

>Tom Grills  tgri...@envantage.com
>Information Services

>Envantage Analytical Software
>15585 NEO Parkway
>Cleveland, OH 44128

Other Threads