简体   繁体   English

C#线程和内存泄漏

[英]C# Threading and Memory Leaks

I am writing an application that opens Microsoft Excel through Interop. 我正在编写一个通过Interop打开Microsoft Excel的应用程序。

The problem I am having is if the application itself Locks Up or Memory Leaks, my application becomes blocked and will not continue the thread. 我遇到的问题是,如果应用程序本身锁定或内存泄漏,我的应用程序将被阻塞,并且将无法继续执行线程。

I have a parent thread that looks at a directory and writes in a loop for each file 我有一个父线程,它查看目录并为每个文件写一个循环

Convert("src.xls","src.pdf",null); Convert(“ src.xls”,“ src.pdf”,null); And some times say for example if we give excel a file type it cannot open it will lock up. 有时说,例如,如果我们给excel一个文件类型,它将无法打开它将锁定。 Which will lock my thread forcing me to have to kill the process. 这将锁定我的线程,迫使我不得不终止该进程。

public static class ExcelConverter
{
    public static bool Convert(string srcFile, string destinationFile, object[] parameters)
    {
        bool bStatus = false;
        Workbook excelWorkBook = null;
        Excel.Application application = null;

        try
        {


        application = new Excel.Application();
        object missingParam = Type.Missing;


            excelWorkBook = application.Workbooks.Open(srcFile);

            if (excelWorkBook != null)
            {
                excelWorkBook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, destinationFile);
            }
            bStatus = true;
        }
        catch (Exception)
        {

            bStatus = false;
        }
        finally
        {
            if (excelWorkBook != null)
            {
                excelWorkBook.Close(false);
                excelWorkBook = null;
            }

            if (application != null)
            {
                application.Quit();
                application = null;
            }

            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }

        return bStatus;
    }
}

for example if we give excel a file type it cannot open it will lock up 例如,如果我们给excel一个文件类型,它将无法打开它将锁定

It is probably trying to display a dialog that tells the user about it. 它可能正在尝试显示一个对话框,以告知用户有关该对话框的信息。 Debug this by setting application.Visible = true so you can actually see the dialog. 通过设置application.Visible = true进行调试,这样您就可以实际看到对话框。 Fix it by specifying more arguments in the Open() call. 通过在Open()调用中指定更多参数来修复它。 The Password, Notify and CorruptLoad arguments have an effect. Password,Notify和CorruptLoad参数起作用。 Screening the files better is an obvious workaround, Excel is really designed to be interactive and chatty about problems. 更好地筛选文件是一个明显的解决方法,Excel的设计确实是交互式的,并且可以解决问题。

You don't have too much to fear from threads, Excel is a single-threaded COM object and COM makes sure that the interface methods are called in a thread-safe way. 您不必担心线程过多,Excel是单线程COM对象,并且COM确保以线程安全的方式调用接口方法。 Which is does in your case by actually creating a new thread to give the interop object a safe home. 在您的情况下,实际上是通过创建一个线程为互操作对象提供一个安全的家来实现的。

Here you may want to create an object that extends MarshalByRefObject object that you can spawn in a new AppDomain to do the conversion. 在这里,您可能想要创建一个扩展MarshalByRefObject对象的对象,您可以在新的AppDomain生成该对象以进行转换。 When done, just unload the AppDomain and all the memory will be cleared. 完成后,只需卸载AppDomain ,所有内存将被清除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM