Solved

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

Posted on 2012-04-06
2
350 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 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

763 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