所以我一直有一個問題,我得到一個錯誤,指出我內存不足。我有一個主線程和一個工作線程,它使用一個名爲SqlLiteAssetCommands.cs的文件,其中包含一些如下所示的命令。C#SQLite Windows CE「內存不足異常。」
Database newConnection;
/// <summary>
/// Open connection return the SQLlight command
/// </summary>
/// <returns></returns>
private SQLiteCommand openConnection()
{
newConnection = new Database();
SQLiteCommand command = new SQLiteCommand();
try
{
newConnection.OpenConnection();
command = newConnection.Connection.CreateCommand();
}
catch (Exception e)
{
log.Error("Could not open connection.", e);
}
return command;
}
/// <summary>
/// Close connection to the local database.
/// </summary>
private void closeConnection()
{
try
{
newConnection.CloseConnection();
}
catch (Exception e)
{
log.Error("Could not close the connection.", e);
}
}
/// <summary>
/// Creates a transaction.
/// </summary>
/// <returns>Returns the new transaction that is created.</returns>
private SQLiteTransaction getTransaction()
{
SQLiteTransaction sqlTransaction = null;
try
{
sqlTransaction = newConnection.Connection.BeginTransaction();
}
catch (Exception e)
{
log.Error("Could not get sqlTransaction.", e);
}
return sqlTransaction;
}
#region Asset Database commands
/// <summary>
/// Delete all rows from sqlite database.
/// </summary>
/// <returns>Returns "Completed" if successful.</returns>
public String deleteAllRows()
{
SQLiteCommand command = openConnection();
String status = null;
using (SQLiteTransaction sqlTransaction = getTransaction())
{
try
{
command.CommandText = @"DELETE FROM Asset";
using (SQLiteDataReader reader = command.ExecuteReader())
{
if (reader.HasRows)
{
reader.Read();
}
reader.Close();
}
status = "Completed";
sqlTransaction.Commit();
}
catch (Exception e)
{
log.Error("Could not delete all assets.", e);
}
}
closeConnection();
return status;
}
所以我開的連接是從所謂的Database.cs不同的文件名爲和錯誤被卡在打開的功能,說明我的內存不足。我不確定我需要做些什麼才能避免發生這種情況,因爲我無法強制發生錯誤。這一次發生在我試圖強迫錯誤顯示的時候,所以我可以屏幕拍攝它,最後厭倦了嘗試和等待而沒有做任何事情。然後它只是由於我的工作線程發生。
public SQLiteConnection Connection = new SQLiteConnection();
// Define a static logger variable so that it references the name of your class
private static readonly ILog log = LogManager.GetLogger(typeof(Database));
// Try to open a connection to the sqlLight database.
public void OpenConnection()
{
try
{
Connection = new SQLiteConnection("Data Source=" + Utilities.Global.SqlLiteDB);
Connection.Open();
}
catch (Exception eErr)
{
log.Error("Error connecting to database.", eErr);
MessageBox.Show("Error connecting to database.", "Error", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
}
}
// Close the database connection.
public void CloseConnection()
{
Connection.Close();
}
}
什麼我可以做些什麼來解決這個問題或改善我的代碼將是有益的。
這是我的工作線程的代碼,因爲這可能是問題的根本原因。
class AssettoServerThread
{
// Define a static logger variable so that it references the name of your class
private static readonly ILog log = LogManager.GetLogger(typeof(AssettoServerThread));
WebServicesSyncAsset syncAsset = new WebServicesSyncAsset();
SqlLightAssetCommands assetToSql = new SqlLightAssetCommands();
/////////////////////// Variables //////////
private volatile bool _shouldStop = false;
// SINGLETON ///////////////////////////////
private static AssettoServerThread instance = null;
public static AssettoServerThread GetInstance()
{
if (instance == null)
instance = new AssettoServerThread();
return instance;
}
// /////////////////////////////////////////
// this variable will hold the condition of ServicesAvailable set by the worker thread
private bool areServicesAvailable = false;
private bool AreServicesAvailable
{
get { return areServicesAvailable; }
set { areServicesAvailable = value; }
}
// This method will be called when the thread is stopped.
public void RequestStop()
{
_shouldStop = true;
}
// Create a worker thread and then check if the webservices are available.
// If available then set ServicesAvailable to true and begin sending updated assets to the server.
public void DoThreading()
{
// Continuous loop
while (!_shouldStop)
{
try
{
// Creates an HttpWebRequest for the specified URL.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(AppConfigSettings.serverIP);
// Set some reasonable limits on resources used by this request
request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version10;
// Sends the HttpWebRequest and waits for a response.
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
//request.Abort();
if (response.StatusCode == HttpStatusCode.OK)
{
areServicesAvailable = true;
}
// Releases the resources of the response.
response.Close();
}
}
catch (WebException ex)
{
areServicesAvailable = false;
log.Info("Website is currently not accessible. Therefore the services are not accessible.", ex);
}
catch (Exception ex)
{
areServicesAvailable = false;
log.Info("Website is currently not accessible. Therefore the services are not accessible.", ex);
}
// If the webservices are available then run else sleep for 30 seconds.
if (AreServicesAvailable)
{
try
{
assetToSql.AddAssetsToServer();
Thread.Sleep(2000);
}
catch (Exception e)
{
log.Error("Could not add asset to server.", e);
}
}
}
}
}
在我的主題上添加了更多信息。也許你可以看到問題。 –