?
Solved

Close Excel instance when Windows Form is closed by user in a VB 2008 application

Posted on 2012-04-06
2
Medium Priority
?
355 Views
Last Modified: 2012-06-22
I'm currently developing an application using Visual Basic, in Visual Studio 2008 Professional, on a Windows XP machine. However, we have both XP and Windows 7 client workstations in our office.

In my application, I'm exporting the results displayed in a DataGridView control to MS Excel, using the methods in the Microsoft.Office.Interop.Excel namespace. The procedure which opens the instance of Excel and exports the data is running in a separate thread from the form which contains the DataGridView control. This is working well.

However, if the user closes the form before the export is completed I want to cancel the thread and close the instance of Excel without saving it. I can use the Thread.Abort method in my form's FormClosing event handler to kill the thread, but the Excel process is still hanging out there even after the application is closed.

What would be the best way to identify and kill that Excel process if the user closes the form prior to the export being completed?
0
Comment
Question by:JeffEdmunds
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 2000 total points
ID: 37816621
I don't think there is any event on the thread which would indicate thread being aborted.

One option is to declare the Excel app variable in common place such as module. Thread then populates it. When calling thread.abort, you can dispose that app variable after aborting thread.
0
 
LVL 1

Author Closing Comment

by:JeffEdmunds
ID: 37816736
You nailed it... I had the declaration inside the module that was being called by the separate thread. Declaring within the form's Declarations section gave it the necessary scope.

Private xlApp As Excel.Application
Private myThread As Thread
....

Then in the FormClosing event handler:

If Not myThread.Equals(Nothing) Then
    If myThread.IsAlive Then
        myThread.Abort()
        xlApp.Quit()
    End If
End If

Seems to be working well - thanks!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question