here is my current code: I have a function that builds an excel file. The file only has one workbook and one worksheet. this process takes about 10 minutes.
I would like to allow user to cancel this process, without saving the file. but if I cancel the program excel.exe lingers in task manager. How do I quit the program elegantly?
here is my simplified code:
Dim oExcel As Excel.Application = New Excel.Application
oExcel.Workbooks.Add()
Dim sht As New Excel.Worksheet
sht = oExcel.Worksheets.Add
if user cancel the function here is my code: to try to exit elegantly
If BackgroundWorker1.CancellationPending Then
e.Cancel = True
releaseObject(sht)
releaseObject(oExcel.ActiveWorkbook)
releaseObject(oExcel)
oExcel = Nothing
GC.Collect()
Exit Sub
End If
this is the releaseObject function
Shared Sub releaseObject(ByVal obj As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
obj = Nothing
Catch ex As Exception
obj = Nothing
End Try
End Sub