public void Dispose() { GC.SuppressFinalize(this); // communicate with COM in a separate thread Task.Run(() => { try { //_wordApp?.Documents.Close(false); _wordApp?.Quit(false); _wordApp = null; _excelApp?.Workbooks.Close(); _excelApp?.Quit(); _excelApp = null; _powerpointApp?.Quit(); _powerpointApp = null; } catch (Exception e) { Debug.WriteLine(e.Message); Debug.WriteLine(e.StackTrace); } }) .Wait(); }
private void LoadApplication() { var succeeded = false; // communicate with COM in a separate thread Task.Run(() => { try { switch (FileType) { case FileTypeEnum.Word: _wordApp = new Microsoft.Office.Interop.Word.Application(); _wordApp.Documents.Add(_path); succeeded = true; break; case FileTypeEnum.Excel: _excelApp = new Application(); _excelApp.Workbooks.Add(_path); succeeded = true; break; case FileTypeEnum.PowerPoint: _powerpointApp = new Microsoft.Office.Interop.PowerPoint.Application(); _powerpointApp.Presentations.Open(_path); succeeded = true; break; default: throw new NotSupportedException($"{_path} is not supported."); } } catch (Exception e) { Debug.WriteLine(e.Message); Debug.WriteLine(e.StackTrace); } }) .Wait(); if (!succeeded) { Dispose(); } }