A
Adam
Hello, I observe a strange behaviour of SqlCeConnection disposing.
I have a simple class to connect to DB with one static SqlCeConnection
namespace SOMETHING
{
/// <summary>
/// This singleton class holds the common application values.
/// </summary>
sealed class Common : IDisposable
{
// Singleton instance variable (Values)
public static readonly Common Values = new Common();
private Common()
{
Load();
}
public void Load()
{
this.databaseConnection = new
SqlCeConnection(LocalConnectionString);
}
public void DbOpen()
{
if (this.databaseConnection.State != ConnectionState.Open)
this.databaseConnection.Open();
}
#region Properties
private SqlCeConnection databaseConnection;
public SqlCeConnection DatabaseConnection
{
get { return databaseConnection; }
set { databaseConnection = value; }
}
public string LocalConnectionString
{
get { return "Data Source=" + localDbPath + localDbName + ";
Password = test"; }
}
public string AppPath
{
get { return
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
}
}
#endregion
#region Dispose
public void Dispose()
{
if(this.databaseConnection.State != ConnectionState.Closed)
this.databaseConnection.Close();
if (this.databaseConnection != null)
{
this.databaseConnection.Dispose();
this.databaseConnection = null;
}
//czy zapisywac zmiany przy usuwaniu obiektu?
//Save();
}
#endregion
~Common()
{
//Dispose();
}
}
}
and from Form1.cs code I do
Common.Values.DbOpen();
in Form1 on load event
and
Common.Values.Dispose();
in Form1 closing event
and everything is ok. But when I uncomment Dispose in Common destructor and
not use Common.Values.Dispose() in Form1 closing event application hang on
this.databaseConnection.Close() in Dispose function. Why?
I have a simple class to connect to DB with one static SqlCeConnection
namespace SOMETHING
{
/// <summary>
/// This singleton class holds the common application values.
/// </summary>
sealed class Common : IDisposable
{
// Singleton instance variable (Values)
public static readonly Common Values = new Common();
private Common()
{
Load();
}
public void Load()
{
this.databaseConnection = new
SqlCeConnection(LocalConnectionString);
}
public void DbOpen()
{
if (this.databaseConnection.State != ConnectionState.Open)
this.databaseConnection.Open();
}
#region Properties
private SqlCeConnection databaseConnection;
public SqlCeConnection DatabaseConnection
{
get { return databaseConnection; }
set { databaseConnection = value; }
}
public string LocalConnectionString
{
get { return "Data Source=" + localDbPath + localDbName + ";
Password = test"; }
}
public string AppPath
{
get { return
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
}
}
#endregion
#region Dispose
public void Dispose()
{
if(this.databaseConnection.State != ConnectionState.Closed)
this.databaseConnection.Close();
if (this.databaseConnection != null)
{
this.databaseConnection.Dispose();
this.databaseConnection = null;
}
//czy zapisywac zmiany przy usuwaniu obiektu?
//Save();
}
#endregion
~Common()
{
//Dispose();
}
}
}
and from Form1.cs code I do
Common.Values.DbOpen();
in Form1 on load event
and
Common.Values.Dispose();
in Form1 closing event
and everything is ok. But when I uncomment Dispose in Common destructor and
not use Common.Values.Dispose() in Form1 closing event application hang on
this.databaseConnection.Close() in Dispose function. Why?