Change behavior and throw a null reference exception

I made this program 2 hours ago and it ran well when I came across this with a programmed .xls file. But when I closed it and started a new instance, it started throwing an exception exception due to failure, why? Plz explain.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;




using System.IO;

using System.Threading;
using Microsoft.Office.Interop;
using Excel = Microsoft.Office.Interop.Excel;


namespace svchost
{

    class MainClass
    {
        Excel.Application oExcelApp;


        static void Main(string[] args)
        {
            MainClass mc = new MainClass();
            while (true)
            {
                if (mc.chec())
                {
                    Console.WriteLine("RUNNING");

                    Thread.Sleep(4000);
                }
                else
                {
                    Console.WriteLine("NOT RUNNING");

                    Thread.Sleep(8000);
                }

            }


        }
        public bool chec()
        {



            try
            {
                oExcelApp = (Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
                Excel.Workbook xlwkbook = (Excel.Workbook)oExcelApp.ActiveWorkbook;

                //****PROBLEM FROM HERE*********

            Console.WriteLine(xlwkbook.Name + "\n");
            ke kw = new ke(ref oExcelApp,ref xlwkbook);

            Console.WriteLine(xlwkbook.Author);

            xlwkbook = null;
        }
        catch (Exception ec)
        {
            oExcelApp = null;
            System.GC.Collect();
            Console.WriteLine(ec);
            return false;

        }
        oExcelApp = null;

        System.GC.Collect();
        return true;
    }
}




class ke

{
    public ke(ref Excel.Application a1, ref Excel.Workbook b1)
    {
        Excel.Worksheet ws = (Excel.Worksheet)a1.ActiveSheet;
        Console.WriteLine(a1.ActiveWorkbook.Name + "\n" + ws.Name);
        Excel.Range rn;
        rn = ws.Cells.Find("657/07", Type.Missing, Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart,
   Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext, false, Type.Missing, Type.Missing);
        Console.WriteLine(rn.Text);

    }

}

}
+2
source share
2 answers

Wow, there are a lot of scary things.

  • Never write a line GC.Collect()unless you have a good reason. This is not one of those times.

  • oExcelApp = null . , "" VB script/app, Set xxx = Nothing, , GC "" GC.Collect(). GC , , , .

  • Exception ... , , , . , , finally, return true try return false finally.

  • ref , - . .

  • . , ?

  • null, , , . . - , rn = ws.Cells.Find - null. ActiveWorkbook null, ke, , . , Marshal.GetActiveObject null, , .

  • , . , ke - !. , ke , .

  • , . , , , VB. , , Range rn = ....

  • Thread.Sleep, - , .

, (, №6) ...

+5
     public bool chec()
            {
                Excel.Application oExcelApp;


                try
                {

                    oExcelApp = (Excel.

Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application"); ;
                if (oExcelApp.ActiveWorkbook != null)
                {
                    Excel.Workbook xlwkbook = (Excel.Workbook)oExcelApp.ActiveWorkbook;


                    ke k = new ke(ref oExcelApp, ref xlwkbook);

                }


            }
            catch 
            {
                if (reg > 100) { } else { reg++; goto End; }//public static int reg=0;
                oExcelApp = null;

                /*Process[] ppo = Process.GetProcessesByName("EXCEL");
                foreach(Process pppp in ppo)
                {
                  pppp.Kill();
                }*/

                End:
                return false;

            }

               finally{ oExcelApp = null;
                System.GC.Collect();}




            return true;
        }
    }
0

Source: https://habr.com/ru/post/1750078/


All Articles