473,763 Members | 7,044 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# problem. Process Excel.exe not killed when application quit

2 New Member
Hi,

I'm new to C#. I'm writing a program that open a new excel file, write into it and lastly close the file. But the program is that there will be a orphaned process named EXCEL.EXE left on the process queue whenever the program quit. Anyway to terminate it automatically when the application exit? I read some previous postings on the same problem and try to use Marshal.Release COMObject and Garbage Collection. None of these works. Here's my coding:

Expand|Select|Wrap|Line Numbers
  1. SaveFileDialog s_Lvl_Max = newSaveFileDialog();
  2. s_Lvl_Max.Filter = "xls files (*.xls)|*.xls|All files (*.*)|*.*";
  3. s_Lvl_Max.FilterIndex = 1;
  4. s_Lvl_Max.FileName = "filenamekk" + "_" + comboBox_baseTP.Text;
  5. s_Lvl_Max.OverwritePrompt = true;
  6. if (s_Lvl_Max.ShowDialog() == DialogResult.OK)
  7. {
  8. Excel.Application oXL;
  9. Excel._Workbook oWB;
  10. Excel._Worksheet oSheet;
  11. try
  12. {
  13. //Start Excel and get Application object.
  14. oXL = new Excel.Application();
  15. oXL.Visible = false;
  16. oXL.DisplayAlerts = false;
  17. //Get a new workbook.
  18. oWB = (Excel._Workbook)(oXL.Workbooks.Add(Missing.Value));
  19. oSheet = (Excel._Worksheet)oWB.Worksheets[1];
  20. //Add table headers going cell by cell.
  21. oSheet.Cells[2, 1] = "Level ";
  22. oSheet.Cells[4, 1] = "Test";
  23. oSheet.Cells[4, 2] = "Frequency";
  24. oSheet.Cells[4, 3] = "Spec";
  25. oSheet.Cells[4, 4] = "Actual";
  26. oSheet.Cells[4, 5] = "Guard";
  27. //Format A11 as bold, vertical alignment = center.
  28. oSheet.get_Range("A1", "E1").Font.Bold = true;
  29. oSheet.get_Range("A1", "E1").VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;
  30. // Create an array to multiple values at once.
  31. int i = 5;
  32. foreach (DataRow dr in dtable2.Rows)
  33. {
  34. oSheet.Cells[i, 1] = dr[0];
  35. oSheet.Cells[i, 2] = dr[1];
  36. oSheet.Cells[i, 3] = dr[2];
  37. oSheet.Cells[i, 4] = dr[3];
  38. oSheet.Cells[i, 5] = dr[4];
  39. i++;
  40. }
  41. i--;
  42. //AutoFit columns A.
  43. oSheet.get_Range("A1", "E1").EntireColumn.AutoFit();
  44.  
  45. oWB.SaveAs(s_Lvl_Max.FileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
  46. Missing.Value, Excel.XlSaveAsAccessMode.xlExclusive, Missing.Value,
  47. Missing.Value, Missing.Value, Missing.Value, Missing.Value);
  48. // Need all following code to clean up and extingush all references!!!
  49. oWB.Close(null, null, null);
  50. oXL.Workbooks.Close();
  51. oXL.Quit();
  52. //System.Runtime.InteropServices.Marshal.ReleaseComO bject(oRng);
  53. System.Runtime.InteropServices.Marshal.ReleaseComObject(oXL);
  54. System.Runtime.InteropServices.Marshal.ReleaseComObject(oSheet);
  55. System.Runtime.InteropServices.Marshal.ReleaseComObject(oWB);
  56. oSheet = null;
  57. oWB = null;
  58. oXL = null;
  59. GC.Collect(); // force final cleanup!
  60. MessageBox.Show(s_Lvl_Max.FileName + " has been successfully saved. ", "File Saved");
  61. }
  62. catch (Exception theException)
  63. {
  64. String errorMessage;
  65. errorMessage = "Error: ";
  66. errorMessage = String.Concat(errorMessage, theException.Message);
  67. errorMessage = String.Concat(errorMessage, " Line: ");
  68. errorMessage = String.Concat(errorMessage, theException.Source);
  69. Console.WriteLine(errorMessage, "Error");
  70. }
  71. }
  72.  
Any other way???
Oct 7 '08 #1
1 6981
l034n
15 New Member
You'll need to kill excel, as it's not quitting, even if you call Quit().

Expand|Select|Wrap|Line Numbers
  1. private void KillExcel()
  2. {
  3.     Process[] excelProcesses = System.Diagnostics.Process.GetProcessesByName("Excel");
  4.  
  5.     foreach(Process p in excelProcesses)
  6.     {
  7.         p.Kill();
  8.     }
  9. }
  10.  
Well, that will effectively kill all Excel processes, so if you have opened 10 excel instances outside your app, when you call this function all of them will close. If you want to prevent it, you should check for PID's.
Oct 7 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
651
by: David Conorozzo | last post by:
I create a new Access.Application instance. As soon as I refernce the property "References", I am unable to issue a truly successful quit on that instance. By that I mean the call to quit is fine but if you look in task manager, the instance is still running. Furthermore, if you opened a database using that instance, you will not be able to open it until you quit the entire application or until you kill that process. BTW, the process is...
8
3372
by: mytfein | last post by:
Hi Everyone, Background: Another department intends to ftp a .txt file from the mainframe, for me to process. The objective is to write a vb script that would be scheduled to run daily to process this .txt file. Goal: I am working on a vba script to:
5
13334
by: Josema | last post by:
Hi to all, I have a windows application that uses workbooks, sheets, ranges, etc... This application is consumed by few clients. Some weeks ago I had problems to kill excel.exe process but i solve with this sentence: System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
10
3072
by: Lars-Erik Aabech | last post by:
Hi! This issue have been discussed a lot, but I haven't found a solution that applies to ASP.NET. I have a library which does some operations on Excel documents, and it will be used in an ASP.NET application. If I use the library in a Forms application, the Excel process is removed when the cleanup operations are done, but with ASP.NET all possible ways to clean up fails. The following code has been tried:
2
5436
by: Praveen K | last post by:
I have a problem in communicating between the C# and the Excel Interop objects. The problem is something as described below. I use Microsoft Office-XP PIA dll’s as these dll’s were been recommended by many for web applications. I create the instances of Excel, Workbook and the worksheet. And later on Release the references by “System.Runtime.InteropServices.Marshal.ReleaseComObject(Object)” and making the object as null finally....
16
4552
by: LP | last post by:
Hello, I am trying to use .NET with Excel. I installed Office 2003 and selected ..NET programming suport option, so it installed all those PIA, as MS sugests. But I can not find a way to destroy Excel process, it still hangs in the taks manager' Processes as running. I am trying very simple code (see below), but Excel wont go away, I tried just about anything I could find on MSDN or by Google. Even calling WIN32 API to destroy Excel. But...
6
2296
by: Darrell Wesley | last post by:
A VB2003 application upgraded to VB2005 that builds an Excel spreadsheet. Everything appears to work correctly except that the Excel object does not go away it is still in the Process list in task manager. This problem did not exist in the VB2003 version. Are there a any ideas of what might be happening? All of the Excel objects are being disposed of by setting them to Nothing and the Quit method is called for Excel. I've even used...
0
2254
by: henning.friese | last post by:
Hello NG, I'm need to write some code which creates tiff files from various document types (doc, pdf, xls). I want to do this by ShellExecuting (via System.Diagnostics.Process) the doc-files with the "print"-verb. This works for me. However, I need to handle the case when a printing application doesnt't quit in a given timespan (for example a password-protected word-document). So I've tried the following:
16
2704
by: alexia.bee | last post by:
Hi all, In some weird reason, excel instance won;t die if i remove the comment from 4 lines of setting values into struct. here is a snipcode public System.Collections.Generic.List<frmMain.sDBTest> LoadTestSet(string TestSetFile, System.Collections.Generic.List<frmMain.sDBTestDBviewList)
0
9386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10144
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9997
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8821
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7366
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3522
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.