Someone uses C # To write winform.

Source: Internet
Author: User
Use C # To write winform, write the data in the Excel file to the database, and export the data in the DataGrid to the Excel format. The Excel Memory leakage is finally found. When the application does not exit, there is always an Excel process that cannot be cleared! Finding answers online is useless! No matter what the process of killing the Excel file is, it is not the most effective method! The most effective method is the following:

1. Create a function for Excel operations and call this function. Calling GC. Collect () in the function is useless because GC does not recycle the code block that calls itself!
2. Call the GC. Collect (); statement under the function. You will find that the Excel process is gone!
For example:
Private void import (){
Excel. Application myexcel = new excel. Application ();
Myexcel. workbooks. Add (openfiledialog1.filename );
//........
// Read the Excel file and import it to the database.
// Clear the Excel spam Process
Myexcel. workbooks. Close ();
Myexcel. Quit ();
System. runtime. interopservices. Marshal. releasecomobject (myexcel );
Myexcel = NULL;
}
Private void excelimport (){
Import ();
GC. Collect ();
}
// Press the button1 button below to read the Excel file using multiple threads and import it to the database.
Private void button#click (Object sender, system. eventargs e ){
If (openfiledialog1.showdialog () = dialogresult. OK ){
System. Threading. thread t = new system. Threading. Thread (new system. Threading. threadstart (excelimport ));
T. Start ();
}
}

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.