标签: dispose

在 UserControl 上实现 Dispose(bool)

当 VS 设计器已经使用DebuggerNonUserCode属性实现它时,如何在 UserControl 上实现 Dispose(boolean) ?我对这个方法的修改会被删除吗?

(来自 UserControl.Designer.vb 的代码)

<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Run Code Online (Sandbox Code Playgroud)

.net user-controls dispose visual-studio-designer

0
推荐指数
1
解决办法
2105
查看次数

在 MessageQueue 上调用 Close() 或 Dispose() 没有任何作用

我有打电话的问题Close()Dispose()MessageQueue。我有一个应该能够管理这些消息队列的应用程序。打开和关闭它们。

但是当我打电话时Close()还是Dispose()什么也没有发生。队列仍然存在于机器上。我使用该MessageQueue.GetPrivateQueuesByMachine()方法获取MessageQueue's的数组,并且在调用Close()or后它仍然在那里Dispose()。经过一番阅读后,似乎Dipose()应该是我使用的那个,但仍然如此。

这是我用来尝试关闭队列的代码

public String CloseQueue(String path)
{
    if (GetQueueArray().Length == 0)
    {
        return "The Queue Array is Empty!";
    }

    foreach (MessageQueue m in GetQueueArray())
    {
        if (m.Path.Equals(path))
        {
            try
            {
                path = m.QueueName;
                m.Close();
                break;
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }
    }
    return "Successfully Closed Queue at path: " + path;
}
Run Code Online (Sandbox Code Playgroud)

该方法GetQueueArray()定义为(在同一类中):

public …
Run Code Online (Sandbox Code Playgroud)

c# dispose msmq

0
推荐指数
1
解决办法
1588
查看次数

CA2000:处置对象警告

我有以下方法:

    public byte[] HtmlToDoc(string hmtl, string userId)
    {
        byte[] data;
        var auditor = new ServiceAuditor
        {
            User = userId
        };
        try
        {
            using (var tx = new ServerText())
            {
                tx.Create();
                tx.Load(Server.HtmlDecode(hmtl), StringStreamType.HTMLFormat);
                tx.Save(out data, BinaryStreamType.MSWord);
            }
        }
        catch (Exception e)
        {
            auditor.Errormessage = e.Message + "/n " + e.StackTrace;
            data = new byte[0];
        }
        finally
        {
            auditor.Save();
            auditor.Dispose();
        }
        return data;
    }
Run Code Online (Sandbox Code Playgroud)

我在编译期间收到以下警告:

警告 CA2000:Microsoft.Reliability:在方法“DocCreator.HtmlToDoc(string, string)”中,对象“new ServiceAuditor()”未沿所有异常路径处理。在对象“new ServiceAuditor()”上调用 System.IDisposable.Dispose 在对它的所有引用都超出范围之前。

奇怪的是,即使我正在处理该对象,我也不明白为什么它会抱怨。你能指出问题出在哪里吗?

c# warnings dispose fxcop

0
推荐指数
1
解决办法
1346
查看次数

如何释放C#中List分配的内存

我有这样的代码:

var myList = db.Table1.ToList();
/*doing some operations on the list*/
var myList = db.Table2.ToList();
/*again doing some operations on the list*/
var myList = db.Table3.ToList(); // I'm getting out of memory exception here.
Run Code Online (Sandbox Code Playgroud)

我不能按页检索数据,因为我需要一次所有的表.在加载另一个表之前,如何处理(我的意思是释放该列表所公开的空间)列表?谢谢.

编辑:

我加载后,实际上是从myList生成了很多(有时是数千个)子列表.所以我真的需要学习如何释放列表.

编辑2:这是我的完整Stacktrace:

   at System.Collections.Generic.List`1.set_Capacity(Int32 value)
   at System.Collections.Generic.List`1.EnsureCapacity(Int32 min)
   at System.Collections.Generic.List`1.Add(T item)
   at System.Data.Entity.Core.Objects.EntityEntry.TakeSnapshot(Boolean onlySnapshotComplexProperties)
   at System.Data.Entity.Core.Objects.Internal.SnapshotChangeTrackingStrategy.TakeSnapshot(EntityEntry entry)
   at System.Data.Entity.Core.Objects.Internal.EntityWrapper`1.TakeSnapshot(EntityEntry entry)
   at System.Data.Entity.Core.Objects.ObjectStateManager.AddEntry(IEntityWrapper wrappedObject, EntityKey passedKey, EntitySet entitySet, String argumentName, Boolean isAdded)
   at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly[TEntity](Func`2 constructEntityDelegate, EntityKey entityKey, EntitySet entitySet)
   at System.Data.Entity.Core.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper)
   at System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext()
   at …
Run Code Online (Sandbox Code Playgroud)

c# dispose memory-management list out-of-memory

-1
推荐指数
1
解决办法
973
查看次数

为什么在这个例子中使用会增加内存消耗

通过阅读这篇文章,我有点困惑:

https://www.dotnetperls.com/using

他们演示了调用2个一次性对象,结束了更多的内存使用,而没有usings 的方法,执行相同的指令,使用较低的内存.

你能解释一下为什么在这个例子中使用会增加内存消耗吗?

我想了解在某些情况下是否应该避免使用.

我认为dispose释放记忆是一个好主意,但看起来我错了.

c# garbage-collection dispose using

-2
推荐指数
1
解决办法
114
查看次数

使 using 语句可用于多个一次性对象

我在一个文件夹中有一堆文本文件,它们都应该有相同的标题。换句话说,所有文件的前 100 行应该是相同的。所以我写了一个函数来检查这个条件:

private static bool CheckHeaders(string folderPath, int headersCount)
{
    var enumerators = Directory.EnumerateFiles(folderPath)
        .Select(f => File.ReadLines(f).GetEnumerator())
        .ToArray();
    //using (enumerators)
    //{
        for (int i = 0; i < headersCount; i++)
        {
            foreach (var e in enumerators)
            {
                if (!e.MoveNext()) return false;
            }
            var values = enumerators.Select(e => e.Current);
            if (values.Distinct().Count() > 1) return false;
        }
        return true;
    //}
}
Run Code Online (Sandbox Code Playgroud)

我使用枚举器的原因是内存效率。我没有在内存中加载所有文件内容,而是逐行同时枚举文件,直到发现不匹配,或者检查了所有标题。

注释的代码行很明显我的问题。我想利用一个using块来安全地处理所有枚举器,但不幸的using (enumerators)是不能编译。显然using只能处理一个一次性物品。我知道我可以通过将整个事物包装在一个try-finally块中并最终在内部循环中运行处理逻辑来手动处理枚举器,但这似乎很尴尬。using在这种情况下,是否有任何机制可以使该语句成为可行的选择?


更新

我刚刚意识到我的函数有一个严重的缺陷。枚举器的构造并不稳健。锁定的文件可能会导致异常,而某些枚举器已被创建。这些枚举器不会被处理。这是我想要解决的问题。我在想这样的事情:

var enumerators = Directory.EnumerateFiles(folderPath)
    .ToDisposables(f …
Run Code Online (Sandbox Code Playgroud)

.net c# dispose enumeration

-2
推荐指数
1
解决办法
811
查看次数