Excel Process Identification and kill in C#

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi,
We have an applicaiton which generates multiple Excel files, the applicaiton uses Threads (ThreadPool) to create each Excel file in a separate thread.
Now the issue is, sometimes (Not all times) in our Production server, the applicaiton runs forever and excel file genreration gets stuck.
We added some logs and found that the application gets stuck when Excel tries to open an workbook (we observed no operations was performed by the applicaiton for more than on hour, and once after we killed the Excel file manually the application continued
to execute and completed), so we assumed that Excel shold have encountered some problem during opening the file which is not being reported back to the application. Note: The Excel file is not having any problem, because it is a template file and the same
file is used for creating all the reports - only the data which is populated inside the files is different between the files
As mentioned before since the applciation runs in threaded environment we have used Monitor.wait / pulse to control main /child threads, since Excel is not giving response back to the application one of the threads is stuck waiting for response from Excel
and the main thread is in waiting mode forever.
So we decided to to make the Monitor.wait based on a timeout, so effectively if the Main thread does not get a signal from any of the running threads for more than 5 minutes then the program will kill the open Excel processes.
This will result in an error in the child thread waiting for the Excel response, and this thread completes with an error rather being stuck, now the main thread will get the signal and since all threads are processed it will continue the remaining operations
and notify the administrators that some error occured during the process.
Now the problem we are facing is,identifying the Excel process to kill which is started by our applicaiton, currently we are looping through all Excel Process (GetProcessByname) and checking Owner name and killing, but there is a risk of killing the same
users file opened by a different application.
We have tried various ways (Process.MainWindowHandle, ExcelObject.HWnd, trying to get the process associated with the Excel object after creation, trying to get the Excel object from the process), but we are not able to get the desired result.
Please let us know whether there any other ways to find the excel process which was started by the current appllicaiton.
Note: The kill method in the current application is used anyways in a later stage to clean up any Excel process which is still running, however as this is not the right way to kill the process we are figuring out a different way to kill the files when we
are not getting response from Excel.

View the full article
 
Back
Top