2014-01-21 1 views
1

Excel.ApplicationClass을 IDisposable 인터페이스에 랩핑하여 사용 후 자동으로 닫으려고합니다. 같은 뭔가 내가 가지고있는 다음Excel Interop에서 두 번째 Excel 인스턴스가 예기치 않게 열립니다.

module Excel = 
    type Application() = 
     member private this.excel = new Excel.ApplicationClass() 

     interface IDisposable with 
      member this.Dispose() = 
       this.excel.Quit() 
       Marshal.ReleaseComObject(this.excel) |> ignore 
내가 시작 (내 작업 관리자에서 볼 수)중인 Excel

let func = 
    use ex = new Excel.Application() 
    () 

2 개의 인스턴스 같은 기능에 호출

하지만, 단 하나의 그들 중 다시 닫힙니다. 아무도 내가 여기서 뭘 잘못하고 있다고 말할 수 있습니까?

답변

3

Dispose을 호출 할 때마다 this.excel 속성은 새 Excel 프로세스를 만들고 하나의 프로세스를 만든 다음 즉시 종료하고 Marshal.ReleaseComObject을 호출하기위한 다른 프로세스를 호출합니다. 두 번째 것은 아마도 살아 남을 것입니다.

은 이런 식으로 코드를 변경

:

module Excel = 
    type Application() = 
     let m_excel = new Excel.ApplicationClass() 

     member private this.excel = m_excel 

     interface IDisposable with 
      member this.Dispose() = 
       this.excel.Quit() 
       Marshal.ReleaseComObject(this.excel) |> ignore 
+0

완벽! 정말 고맙습니다! – torbonde