Good day to all! There is a code which from Excel transfers all data to the form in dataGridView1. Naturally, after working with Excel, you need to unload all his junk so that he does not hang in the processes. All code was in button_Click. The upload worked and the process terminated correctly right after execution. It was necessary to transfer all the code to the function and call it on click. Transferred and the process remains hanging in the task processor. Googled and found nothing What am I doing wrong? Code.

private void button3_Click(object sender, EventArgs e) { test(); } public void releaseObject(object obj) { try { System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj); obj = null; } catch (Exception ex) { obj = null; } finally { GC.Collect(); } } public void test() { dsTest dSet = new dsTest(); Excel.Application ExcelApp = new Excel.Application(); Excel.Worksheet ExcelWorkSheet; Excel.Workbook ExcelWorkBook; Excel.Range ExcelRange; var workbooks = ExcelApp.Workbooks; ExcelWorkBook = workbooks.Open(@"D:\\Эксель.xlsm", 0, false); ExcelWorkSheet = ExcelWorkBook.ActiveSheet; ExcelRange = ExcelWorkSheet.UsedRange; for (int Rnum = 2; Rnum <= ExcelRange.Rows.Count - 2; Rnum++) { DataRow dr = dSet.DataTable1.NewRow(); for (int Cnum = 1; Cnum <= ExcelRange.Columns.Count; Cnum++) { if ((ExcelRange.Cells[Rnum, Cnum] as Excel.Range).Value2 != null) { dr[Cnum - 1] = (ExcelRange.Cells[Rnum, Cnum] as Excel.Range).Value2.ToString(); } } dSet.DataTable1.Rows.Add(dr); dSet.DataTable1.AcceptChanges(); } dataGridView1.DataSource = dSet.DataTable1; dataGridView1.Visible = true; ExcelWorkBook.Close(true, null, null); ExcelApp.Quit(); releaseObject(ExcelApp); releaseObject(workbooks); releaseObject(ExcelWorkBook); releaseObject(ExcelWorkSheet); releaseObject(ExcelRange); } 

Previously, all that was in test () was in button3_Click and the process was completed, but in this way it remains to hang.

    2 answers 2

    Found the solution, though I don’t really understand how it is connected. dsTest dSet = new dsTest (); I had this thing in global variables, I removed it and it all worked. I don’t know why it didn’t hurt in the event handler, but suddenly it happened.

      It's simple:

      1. You use Interop.
      2. You do not release certain variables that should be released

      I would not advise in principle to use interop.

      • You can easily leave the running processes through interop

      • In general, it is necessary that it be installed to work with files, otherwise the program will not work

      • the code is pretty complicated

      • as well as the main problem - speed. He's sooooo slow.)

        Here's a link that will explain everything in more detail, as well as suggest alternative ways:

      How easy it is to work with / open / edit / save Excel / CSV files

      If you want to work with interop ... Catch a crutch to close the eksel in any case. It works, even if you write a curve code and do not release the necessary variables.

       [DllImport("user32.dll")] private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId); private Application _excelApp; private void CloseExcelApp() { int hWnd = _excelApp.Application.Hwnd; uint processID; GetWindowThreadProcessId((IntPtr)hWnd, out processID); Process.GetProcessById((int)processID).Kill(); }