C # exit the Excel process after calling the COM Component

Source: Internet
Author: User

When I used the WinForm program to call the Excel COM component, I also encountered the Excel process exit problem. Many people have encountered this problem, and the solution is also very hot. To sum up, the first method is to call the relevant component release method, and then use code to call the Garbage Collector for processing. This method is normal, that is, to exit the process through formal channels. [Csharp] public void QuitExcel (Microsoft. office. interop. excel. application application) {application. quit (); try {System. runtime. interopServices. marshal. releaseComObject (application);} catch (System. exception ex) {MessageBox. show (ex. toString ();} finally {application = null; GC. collect () ;}} the second method is to force the Excel process related to Kill. Because the first method may be related to the relevant environment, and can be successful on some machines, but may not be successful on some machines, we propose a more acute approach, that is, find the process related to the ExcelApplication and force it to kill it. However, after all, this is not a conventional method to exit a process. I don't know if it will cause problems such as memory fragmentation. If not, [csharp] [DllImport ("User32.dll", CharSet = CharSet. auto)] public static extern int GetWindowThreadProcessId (IntPtr hwnd, out int ID); public static void Kill (Microsoft. office. interop. excel. application excel) {excel. quit (); IntPtr t = new IntPtr (excel. hwnd); int k = 0; GetWindowThreadProcessId (t, out k); System. diagnostics. process p = System. Diagnostics. process. getprocpolicyid (k); p. kill ();} another point to be put forward here is that if the application program attached to the COM component to be called exits, whether or not the Excel process is processed Additionally, the Excel process exits. For example, I am using the COM component called by the WinForm program. Now, after I finish processing the Excel file, I did not process the Excel file. Instead, I clicked X in the upper right corner to close the application, and the Excel file exited. However, it should be pointed out that many WinForm programs should not be closed after processing the Excel file, and a lot of additional work is required. Therefore, it is necessary to process the relevant Excel process. In addition, if it is a Web program, it will always run. If it is not processed, the Excel process will always exist.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.