我正在處理多線程wpf應用程序我得到「AccessViolationException」說法試圖讀取或寫入受保護的內存。這通常表明其他內存已損壞。我的ConnectionOpen()中的 。我的代碼如下 。由於多線程連接到db時wpf中的AccessViolation異常
public class DatabaseServices
{
static SQLiteConnection connection;
static object conLock = new object();
static object conCloseLock = new object();
public static SQLiteDataReader ConnectionOpen(string Query)
{
lock (conLock)
{
if (connection != null && connection.State != System.Data.ConnectionState.Open)
{
connection = new SQLiteConnection("Data Source=Database/abc.sqlite");
connection.Open();
}
else if (connection == null)
{
connection = new SQLiteConnection("Data Source=Database/abc.sqlite");
connection.Open();
}
SQLiteCommand mycommand = new SQLiteCommand(Query, connection);
SQLiteDataReader sqlite_datareader = mycommand.ExecuteReader();
return sqlite_datareader;
}
}
public static void ConnectionClose()
{
lock (conCloseLock)
{
connection.Close();
}
}
}
我已經使用鎖以及線程安全代碼,但它不工作。爲什麼?