title: AccessViolation Exception in wpf while connecting to db due to multiple threads
body: <p>I am working on a multithreaded wpf application I get "AccessViolationException" saying Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
  in my ConnectionOpen().
  my code is as follows.</p>
  
  <pre><code>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 &amp;&amp; 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();
          }
      }
  
  }
  </code></pre>
  
  <p>I've used lock as well for thread safe code but its not working.why?</p>