Tag: 析构函数

C#析构函数未按预期工作

请参阅下面的代码。 我希望它打印10,因为我已经显式调用了垃圾收集器。 但我总是输出0或20作为输出。 这是为什么? void Main() { Panda[] forest_panda = new Panda[10]; for(int i=0; i<forest_panda.GetLength(0);i++) { forest_panda[i]=new Panda("P1"); } for(int i=0; i<forest_panda.GetLength(0);i++) { forest_panda[i]=new Panda("P1"); } System.GC.Collect(); Console.WriteLine("Total Pandas created is {0}",Panda.population); } class Panda { public static int population=0; public string name; public Panda(string name) { this.name = name; population = population + 1; […]

处置SQL连接

我有一个SQL类连接到数据库并转发DataTable。 我知道完成后必须处理SqlConnection。 我知道这可以使用using块来完成,但是将Dispose()调用放在这个类的析构函数中也是可以接受的吗? Herre是我的代码: public class SQLEng { //Connection String Property //Must be set to establish a connection to the database public string ConnectionString{ get; set; } SqlConnection _Conn; //Overridden Constructor enforcing the Connection string to be set when created public SQLEng(string connectionString) { ConnectionString = connectionString; _Conn = new SqlConnection(connectionString); } //ensure the SqlConnection […]

类析构函数问题

我正在创建一个包含StreamWrite的简单类 class Logger { private StreamWriter sw; private DateTime LastTime; public Logger(string filename) { LastTime = DateTime.Now; sw = new StreamWriter(filename); } public void Write(string s) { sw.WriteLine((DateTime.Now-LastTime).Ticks/10000+”:”+ s); LastTime = DateTime.Now; } public void Flush() { sw.Flush(); } ~Logger() { sw.Close();//Raises Exception! } } 但是当我在析构函数中关闭此StreamWriter时,它会引发StreamWriter已被删除的exception? 为什么? 以及如何使其工作,以便在删除Logger类时,StreamWriter在删除之前关闭? 谢谢!

析构函数何时在ASP.NET中调用C#类?

说,我有自己的C#类定义如下: public class MyClass { public MyClass() { //Do the work } ~MyClass() { //Destructor } } 然后我从ASP.NET项目创建我的类的实例,如下所示: if(true) { MyClass c = new MyClass(); //Do some work with ‘c’ //Shouldn’t destructor for ‘c’ be called here? } //Continue on 我希望在if范围的末尾调用析构函数,但它永远不会被调用。 我错过了什么?

何时在WCF服务中调用析构函数

我需要创建一个维护WCF会话的服务。 在构造函数中,我从数据库中读取数据,当会话结束时,我必须将其保存回来。 如果我理解正确,当我在客户端上调用Close()时会话结束(我的客户端ServiceClient是使用SvcUtil.exe创建的)。 当我测试它时,我发现它有时在大约后被调用。 10分钟,有时20分钟后,有时甚至根本没有。 那么析构函数何时被调用? 服务 [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)] public class Service:IService { private User m_User = null; public Service() { m_User = User.LoadFromDB(); } ~Service() { m_User.SaveToDB(); } public void SetName(string p_Name) { m_User.Name = p_Name; } } Web.config文件 客户 ServiceClient serviceClient = null; try { serviceClient = new ServiceClient(); serviceClient.SetName(“NewName”); Console.WriteLine(“Name set”); […]

为什么Finalize / Destructor示例在.NET Core中不起作用?

我试图了解终结和析构函数如何在C#中工作,我试图在System.Object.Finalize示例中运行代码(代码复制粘贴,没有做出任何更改),但输出与预期的不一样,它表明从不调用析构函数。 代码是: using System; using System.Diagnostics; public class ExampleClass { Stopwatch sw; public ExampleClass() { sw = Stopwatch.StartNew(); Console.WriteLine(“Instantiated object”); } public void ShowDuration() { Console.WriteLine(“This instance of {0} has been in existence for {1}”, this, sw.Elapsed); } ~ExampleClass() { Console.WriteLine(“Finalizing object”); sw.Stop(); Console.WriteLine(“This instance of {0} has been in existence for {1}”, this, sw.Elapsed); […]

我应该将entity framework视为非托管资源吗?

我正在使用一个在其构造函数中使用EF引用的类。 我已经实现了IDisposable ,但我不确定我是否需要析构函数,因为我不确定我是否可以将EF归类为非托管资源。 如果EF是托管资源,那么我不需要析构函数,所以我认为这是一个恰当的例子: public ExampleClass : IDisposable { public ExampleClass(string connectionStringName, ILogger log) { //… Db = new Entities(connectionStringName); } private bool _isDisposed; public void Dispose() { if (_isDisposed) return; Db.Dispose(); _isDisposed= true; } } 如果EF不受管理,那么我将继续这样做: public ExampleClass : IDisposable { public ExampleClass(string connectionStringName, ILogger log) { //… Db = new Entities(connectionStringName); } public […]

内部.Net Framework数据提供程序错误1

我正在使用Visual Studio 2012 Ultimate版本开发一个带有所有Service Pack,C#和.NET Framework 4.5的WinForm应用程序。 我得到这个例外: Internal .Net Framework Data Provider error 1 有了这个堆栈: en System.Data.ProviderBase.DbConnectionInternal.PrePush(Object expectedOwner) en System.Data.ProviderBase.DbConnectionPool.PutObject(DbConnectionInternal obj, Object owningObject) en System.Data.ProviderBase.DbConnectionInternal.CloseConnection(DbConnection owningObject, DbConnectionFactory connectionFactory) en System.Data.SqlClient.SqlConnection.CloseInnerConnection() en System.Data.SqlClient.SqlConnection.Close() en AdoData.TRZIC.DisposeCurrentConnection() en AdoData.TRZIC.Finalize() 在析构函数中: ~TRZIC() { DisposeCurrentConnection(); if (this.getCodeCmd != null) this.getCodeCmd.Dispose(); } private void DisposeCurrentConnection() { if (this.conn != null) […]

析构函数 – 如果应用程序崩溃,它会被调用吗?

如果应用程序崩溃,是否会调用析构函数? 如果它是一个未处理的exception,我猜它确实如此,但更严重的错误,或者用户杀死应用程序进程之类的东西呢? 还有一些潜在的愚蠢问题: 当应用程序退出并且所有终结器都已执行时,应用程序中的所有对象会发生什么 – 对象是否被垃圾收集,或者它们是否以某种方式全部“卸载”了进程或应用程序域? 是每个应用程序的垃圾收集器部分(在同一个进程中运行)还是独立的?

MEF的对象破坏问题

我使用静态变量来保存对象的数量。 在构造函数中,我增加了这个变量。 这样我知道创建了多少个对象实例。 使用这些对象后,它们会被解除引用。 我怀疑MEF是否持有对这些对象的引用,因此我强制GC进行清理(使用GC.Collect()方法)。 我希望在下一个对象创建时,这个变量从零开始,但它从最后一个数字开始重新开始。 我在destructor放置了一个用于跟踪的日志记录机制,只有在应用程序关闭后才会销毁对象。 我可以假设MEF已经创建了对这些对象的其他引用吗? 我使用MEF和ExportFactory来创建我的对象 编辑: 也许应该用ExportLifetimeContext做些什么?