Monday, 15 August 2011

sqlite - Thread synchronization in C#? -


I have very few small functions, each executes a query. I want only one function to run at a time, what is the best way to thread sync to avoid database lock issue in SQLite (C #).

The function is in many classes, how you lock all functions in all DB sections, so that only one function from any DB class is executed.

I will play devil's advocate ... why would you call them in different threads, if at the same time Will one execution? I just keep things in a queue and read one line in a row at the same time.

EDIT: According to my comment below, here is an example of a supporting class that you can use to reactivate your data in a more centralized location. This is a very basic example and there is no test.

  class database {private string connection string; Private Readonly Object syncRoot = new object (); Public database (string connection string) {this.connectionString = connectionString; } Public Zero ExecuteReader (SqlCommand command, verb & lt; idatarecord & gt; forEachRow) {lock (syncRoot) {(var connection = new SqlConnection (connectionString)) {command.Connection = connection; Connection.Open (); (Reader.Read ()) while using {var reader = command.ExecuteReader ()) {forEachRow (reader); }}}}}} Var myDB = new database ("connection string"); Var myCommand = New SqlCommand ("Choose ID from Blah"); MyDB.ExecuteReader (myCommand, line => console.wrightline ("id: {0}", line ["id"]));  

No comments:

Post a Comment