Solved

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

Posted on 2012-04-06
2
345 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
2 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

867 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now