2013-12-10 46 views
0

所以我一直有一个问题,我得到一个错误,指出我内存不足。我有一个主线程和一个工作线程,它使用一个名为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(); 
    } 
} 

enter image description here

什么我可以做些什么来解决这个问题或改善我的代码将是有益的。

这是我的工作线程的代码,因为这可能是问题的根本原因。

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); 
       } 
      } 
     } 
    } 
} 

回答

0

然后,它只是碰巧因为我的工作线程。

然后,您的线程代码可能是相关的。

+0

在我的主题上添加了更多信息。也许你可以看到问题。 –

0

我最终所做的并不是将两个线程放到一个工作线程中,而是确保它不会重叠。我还为我的SQLlite数据库添加了一个连接池。我还更改了项目中的图像并删除了所有不必要的图像以节省空间。我不知道是什么修复了它,但这对我有效。