I have developed a Windows Service in C# that receives events from an external hardware. For each received event, a SQLite database creates a record and save it.
The problem is that from time to time, a DBLock error occurs, so I need to control that only one thread can write to the database at a time.
I have created this class variable that is instantiated in Service OnStart method:
private DataWare.monitorEntities _db;
Then, in the event, I have:
void driver_Transaccion(object sender, AttendanceReader.MarcacionEventArgs e)
{
lock (_db)
{
/* Code that creates a record a does some other actions */
/* ......... */
_db.SaveChanges();
}
}
The problem is that I continue receiving the DBLock exception, so, it seems the instruction lock (_db) is not taken into account.
Any help, please?
Are you disposing your connection after usage?
lock (_lockObj)
{
using (MyEntities context = new MyEntities)
{
// do stuff
context.SaveChanges();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.