How to close the process after using Excel (C #)

Source: Internet
Author: User

When using C # To operate the COM component of Excel, closing the Excel process is very important. There are some methods on the Internet. I have summarized two of them and passed the test.

The first method is to use the. net gc, that is, the garbage collector.CodeAs follows:

Code
1 Using System;
2 Using System. Collections. Generic;
3 Using System. text;
4 Using System. runtime. interopservices;
5
6 Namespace Exceltest
7 {
8 Class Dataoutput
9 {
10 Static   Void Main ( String [] ARGs)
11 {
12 Excel. Application app =   New Excel. applicationclass ();
13 Excel. Workbook wbook = App. workbooks. Add ( True );
14 Excel. worksheet wsheet = Wbook. worksheets [ 1 ] As Excel. worksheet;
15 App. Visible =   True ;
16
17 System. runtime. interopservices. Marshal. releasecomobject (wsheet );
18 System. runtime. interopservices. Marshal. releasecomobject (wbook );
19 System. runtime. interopservices. Marshal. releasecomobject (APP );
20 GC. Collect (); // Explicitly call GC
21
22 Console. Read ();
23 }
24 }
25 }

 

Focus on the last four lines. The system. runtime. interopservices. Marshal class mainly provides operations on the unmanaged memory. For the releasecomobject function, the explanation on msdn is "This Runtime Library can be called with reference count for packaging. Every time the COM interface pointer is mapped to this Runtime library, it can be called for packaging, the reference count increases progressively. The releasecomobject method reduces the reference count that can be invoked by the Runtime Library. When the reference count reaches zero, the runtime releases all references on the unmanaged COM object ." That is, disconnect the reference to the object, so that the GC will find that the object will not be used again, then it will be recycled.

The second method is to kill the process by the process ID. However, some netizens said that the first method sometimes does not work, because I simply use the console for testing and may not be able to do so in other cases. Therefore, the second method is recommended.

Code
1 Namespace Exceltest
2 {
3 Class Dataoutput
4 {
5 Static   Void Main ( String [] ARGs)
6 {
7 Excel. Application app =   New Excel. applicationclass ();
8 Excel. Workbook wbook = App. workbooks. Add ( True );
9 Excel. worksheet wsheet = Wbook. worksheets [ 1 ] As Excel. worksheet;
10 App. Visible =   True ;
11
12 Kill (APP );
13
14 Console. Read ();
15 }
16
17 [Dllimport ( " User32.dll " , Charset = Charset. Auto)]
18 Public   Static   Extern   Int Getwindowthreadprocessid (intptr hwnd, Out   Int ID );
19
20 Public   Static   Void Kill (Excel. Application Excel)
21 {
22 Intptr t =   New Intptr (Excel. hwnd ); // Get this handle. The specific function is to get this memory entry.
23
24 Int K =   0 ;
25 Getwindowthreadprocessid (t, Out K ); // Obtain the unique identifier K of the process.
26 System. Diagnostics. PROCESS p = System. Diagnostics. process. getprocpolicyid (k ); // Get reference to process K
27 P. Kill (); // Disable process K
28
29 }
30 }
31 }

 

The getwindowthreadprocessid function is a function in user32.dll used to obtain the process ID of an object through the object handle. The extern modifier is used to declare an external implementation method. After understanding this, the entire process is very simple: in fact, it is to first obtain the handle of the Excel. Application Instance app, then get the process ID through the handle, and finally kill the process of this ID.

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.