|
-
Jan 29th, 2016, 06:41 AM
#1
Thread Starter
Member
Opening Excel Causes Sporadic Hang
I've got a piece of code that creates a new Excel file from an existing file and renames it. It repeats this 100-200 times (the existing file is a log of USB data; the "new" file logs successive iterations of capturing data, FWIW).
The code works fine 99% of the time. But every once in awhile, it completely hangs when attempting to open the "new" Excel workbook (see line with double asterisks). The "new" file gets created and named properly ( I can open it manually, and get a "Read Only" notification), but it simply does not complete the process of opening the workbook. I have two Excel processes running when I open Task Manager (one for the new file, one for the existing), so that seems to make sense. The contents of ResultsFilePath exist and are correct, so no issue there.
But I can't figure out why it's hanging, or even what diagnostics I can wrap around it to capture it when it happens.
If it matters (though I don't see why it should), this is all happening in a BackgroundWorkerTask while my main task updates the UI.
Any help is appreciated.
Code:
Function CopyFormatUSBDatatoReport(ByVal TouchDur As Integer, ByVal TestTime As Integer) 'Create report for each test from temp USB data location
Dim oExcel As Excel.Application
Dim oBook As Excel.Workbook
Dim oSheet As Excel.Worksheet
ResultsFilePath = ControllerSetup.ControllerTestResultFolderPath & TouchDur & "mS_TouchDuration_" & TestTime & "mS_TestTime.xlsx"
My.Computer.FileSystem.CopyFile("C:\QA_Controller_Test_Files\USBCaptureTemp.xlsx", ResultsFilePath) 'Create log file for this touch duration
oExcel = New Excel.Application 'Create a new instance of Excel
**oBook = oExcel.Workbooks.Open(ResultsFilePath)** 'Get correct file
oSheet = oExcel.Worksheets(1)
oExcel.Visible = False 'Don't show it to the user
'Do some other work
oBook.Save()
oBook.Close() 'Disconnect from Excel and end processes
oExcel.Quit()
oBook = Nothing
oExcel = Nothing
GC.Collect()
GC.WaitForPendingFinalizers()
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|