简体   繁体   English

SQLite.net多线程

[英]SQLite.net Multithread

I'm trying to use SQLite in my WPF Application in multithread mode. 我正在尝试在多线程模式下的WPF应用程序中使用SQLite。 I'm beginner in C# and I've read lot of post about SQLite and multithreading but... can't achieve to do what I want... 我是C#的初学者,我已经阅读了很多有关SQLite和多线程的文章,但是...无法实现我想要的...

I have made a backgroundWorker which parse different file and then I want to insert into one Database the result of each file into the database. 我做了一个backgroundWorker,它可以解析不同的文件,然后将每个文件的结果插入一个数据库。

I'm using the SQLite.net PCL . 我正在使用SQLite.net PCL

I will not post all the code except if someone ask me, but to make some test I do something like that : 除了有人问我,我不会发布所有代码,但是要进行一些测试,我会做类似的事情:

lock (_lock)
{
   using (var test = new SQLiteConnection(new SQLitePlatformWin32(), "test.s3db"))
   {
       test.Insert(new MyClassWhichHaveAllField(){Title="XXX"});
   }
}

_lock is defined as : _lock定义为:

public static object _lock = new object();

in my backgroundWorker implementation, so as static members. 在我的backgroundWorker实现中,所以作为静态成员。

Even if I put the lock, I received exception "SQLite.Net.SQLiteException: Could not open database file: recipes.s3db (Misuse)" 即使我锁了,也会收到异常“ SQLite.Net.SQLiteException:无法打开数据库文件:recipes.s3db(滥用)”

I try to add some SQLiteOpenFlags like : 我尝试添加一些SQLiteOpenFlags,如:

SQLiteOpenFlags.FullMutex

It doesn't change anything. 它没有任何改变。

Thanks you very much... 非常感谢你...

Shouldn't you be using the SQLiteAsync.cs wrapper for multi-threaded ? 您不应该对多线程使用SQLiteAsync.cs包装器吗?

SQliteAsyncConnection and Asynchronous API further down the page. SQliteAsyncConnection和异步API在页面的下方。

I answer to my question, because indeed, what I have implemented work well except that in my code I wrote : 我回答我的问题,因为确实,除了我写的代码之外,我实现的效果很好:

static object _lock = new object();

so it was defined as private ... 所以它被定义为私人...

Thanks you. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM