1

for my application I am calling an excel file to read or write data, which is working fine, but I did notice that there are 'Microsoft Excell' processes piling up in my task manager. Although this does not seem to impact the application, it will impact my pc's speed in the long run.

I tried to close excell using the .Quit() method, but this only appears to exit the session while still leaving the process in place.

Here is the context in which I am calling excell:

While reading data:

public List<string> GetLeverancierInfo(int Index)
        {
            List<string> Output = new List<string>();
            int Positie = 0;
            bool DataGrabbing = true;
            DataSheet DataSheet = new DataSheet(@"C:\Users\Melvin\source\repos\ShopITScanner\Shop-IT FactuurScanner\Properties\Resources\DataFile.xlsx", 1);
            while (DataGrabbing)
            {
                string HuidigeCell = DataSheet.ReadCell(Form1.nieuwBedrijf.BedrijfsIndex, Positie);
                if(HuidigeCell != null)
                {
                    Output.Add(HuidigeCell);
                    Positie++;
                }
                else
                {
                    DataGrabbing = false;
                }
            }
            DataSheet.Exit();
            return Output;
        }

The DataSheet.ReadCell() method is as follows:

public string ReadCell(int X, int Y)
        {
            X++;
            Y++;
            if(ProductSheet.Cells[X, Y].Value2 != null)
            {
                return ProductSheet.Cells[X, Y].Value2;
            }
            else
            {
                return null;
            }

        }

The code for writing to Excell:

        public void SetLeverancierInfo(List<string> Attributen)
        {
            DataSheet DataSheet = new DataSheet(@"C:\Users\Melvin\source\repos\ShopITScanner\Shop-IT FactuurScanner\Properties\Resources\DataFile.xlsx", 1);
            int YPositie = 0;
            int XPositie = 0;
            foreach(string Attribuut in Attributen)
            {
                bool Added = false;
                while (!Added)
                {
                    if (DataSheet.ReadCell(XPositie, YPositie) == null)
                    {
                        DataSheet.WriteCell(XPositie, YPositie, Attribuut);
                        Added = true;
                    }
                    else
                    {
                        XPositie++;
                    }
                }


                YPositie++;
            }
            DataSheet.Exit();

        }

The code for DataSheet.WriteCell() is as follows:

public void WriteCell(int X, int Y, string S)
        {
            X++;
            Y++;
            if(ProductSheet.Cells[X, Y].Value2 == null)
            {
                ProductSheet.Cells[X, Y].Value2 = S;
            }
            else
            {
                if(MessageBox.Show("Met de volgende operatie worden uw instellingen overgeschreven. \nDit kan niet ongedaan gemaakt worden. \nWeet u het zeker?", "Waarschuwing", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    ProductSheet.Cells[X, Y].Value2 = S;
                }
            }
            DataFile.Save();
        }

And lastly when I finished my operations with Excell I run this:

        public void Exit()
        {
            Excel.Quit();
        }

When using the .Quit() method, I assumed it would get rid of the entire excell process, but in reality that is not happening. I am not getting errors either, except for an occasional "file is opened by another process" message, which is logical when the processes aren't killed.

Melvin Winthagen
  • 277
  • 1
  • 2
  • 9
  • I've never seen this `DataSheet` type before. What namespace is it in? Did you write it? – mjwills Jun 20 '19 at 07:45
  • @mjwills The DataSheet is the name I gave the class from which excell is called and it contains a path, excel application, workbook and worksheet. What are you missing right now to be able to reproduce it? (Sorry, I am quite new to StackOverflow so I may not be as familiar with all the codes of conduct). I have now. That is some complicated code that will take some time for me to understand but I will look into it. Thanks in advance. – Melvin Winthagen Jun 20 '19 at 08:17
  • Have you considered switching from office interop to Microsoft's [DocumentFormat.OpenXml](https://www.nuget.org/packages/DocumentFormat.OpenXml/) nuget package? It lets you manipulate xlsx files and never opens a instance of excel.exe (you don't even need excel installed on the computer you plan to do this on) – Scott Chamberlain Jun 20 '19 at 08:20
  • It is mostly a debugging artifact, if you stop your program without allowing it to exit normally then you are guaranteed to leave an orphaned excel.exe instance behind. Just keep Task Manager handy. [More here](https://stackoverflow.com/a/25135685/17034). – Hans Passant Jun 20 '19 at 11:01
  • I'd suggest starting with https://stackoverflow.com/a/47852419/34092 from the duplicate. _If you go down the `GC.Collect` route, be sure to run as Release rather than Debug build._ – mjwills Jun 20 '19 at 11:38
  • Just kill EXCEL.exe from task manager... – Prakash Mhasavekar Jul 11 '19 at 17:02

0 Answers0