Solved

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

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

Suggested Solutions

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

738 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