const int loops = 1000;

public void DatabaseThreadSafetyTest()
{
var backgroundThread = new Thread(new System.Threading.ThreadStart(() =>
{
for (int i = 1; i <= loops; i++)
{
Console.WriteLine("Background thread loop " + i);
using (var db = new SQLiteConnection(DbPath, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create | SQLiteOpenFlags.SharedCache)) {
db.Insert (new MyClass());
}
}
}));
backgroundThread.Start(); for (int i = 1; i <= loops; i++)
{
Console.WriteLine("Main thread loop " + i);
using (var db = new SQLiteConnection(DbPath, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create | SQLiteOpenFlags.SharedCache)) {
db.Insert (new MyClass());
}
}
}

 

using System;
using System.Data.SQLite;
using System.Threading.Tasks; namespace SQLiteTest
{
class Program
{
static void Main(string[] args)
{
var tasks = new Task[100]; for (int i = 0; i < 100; i++)
{
tasks[i] = new Task(new Program().WriteToDB);
tasks[i].Start();
} foreach (var task in tasks)
task.Wait();
} public void WriteToDB()
{
try
{
using (SQLiteConnection myconnection = new SQLiteConnection(@"Data Source=c:\123.db"))
{
myconnection.Open();
using (SQLiteTransaction mytransaction = myconnection.BeginTransaction())
{
using (SQLiteCommand mycommand = new SQLiteCommand(myconnection))
{
Guid id = Guid.NewGuid(); mycommand.CommandText = "INSERT INTO Categories(ID, Name) VALUES ('" + id.ToString() + "', '111')";
mycommand.ExecuteNonQuery(); mycommand.CommandText = "UPDATE Categories SET Name='222' WHERE ID='" + id.ToString() + "'";
mycommand.ExecuteNonQuery(); mycommand.CommandText = "DELETE FROM Categories WHERE ID='" + id.ToString() + "'";
mycommand.ExecuteNonQuery();
}
mytransaction.Commit();
}
}
}
catch (SQLiteException ex)
{
if (ex.ReturnCode == SQLiteErrorCode.Busy)
Console.WriteLine("Database is locked by another process!");
}
}
}
}

  

SQLite multiple threads的更多相关文章

  1. Multiple Threads reading from the same file(转载)

    问 I have a xml file that needs to be read from many many times. I am trying to use the Parallel.ForE ...

  2. Android 性能优化(16)线程优化:Creating a Manager for Multiple Threads 如何创建一个线程池管理类

    Creating a Manager for Multiple Threads 1.You should also read Processes and Threads The previous le ...

  3. caffe网络在多线程中无法使用GPU的解决方案 | cpp caffe net run in multiple threads

    本文首发于个人博客https://kezunlin.me/post/8d877e63/,欢迎阅读! cpp caffe net run in multiple threads Guide set_mo ...

  4. bsxfun.h multiple threads backup

    https://code.google.com/p/deep-learning-faces/source/browse/trunk/cuda_ut/include/bsxfun.h?r=7&s ...

  5. Hashtable insert failed. Load factor too high. The most common cause is multiple threads writing to the Hashtable simultaneously

    暂时也没准确定位到问题 https://support.microsoft.com/zh-cn/help/2803754/hotfix-rollup-2803754-is-available-for- ...

  6. 临界区代码 critical section Locks and critical sections in multiple threads

    临界区 在同步的程序设计中,临界区段(Critical section)指的是一个访问共享资源(例如:共享设备或是共享存储器)的程序片段,而这些共享资源有无法同时被多个线程访问的特性. 当有线程进入临 ...

  7. PatentTips - Controlling TSC offsets for multiple cores and threads

    BACKGROUND Many processors include a time stamp count (TSC) counter which is typically implemented a ...

  8. 【腾讯Bugly干货分享】微信iOS SQLite源码优化实践

    本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/57b58022433221be01499480 作者:张三华 前言 随着微信iO ...

  9. SQLite源程序分析之sqlite3.c

    /****************************************************************************** ** This file is an a ...

随机推荐

  1. MobaXterm v10.9破解

    去官网下载个人版 Exeinfo查壳发现无壳 载入OD,右键,字符串智能搜索. Ctrl+F搜索关键词About,找到到FormAbout处,即关于窗体的创建和显示的位置.双击查看汇编代码 程序在窗体 ...

  2. window、Linux 文本文件转换

    如下: $ sed -e 's/.$//' mydos.txt > myunix.txt $ sed -e 's/$/\r/' myunix.txt > mydos.txt

  3. mysql问题解决SELECT list is not in GROUP BY clause and contains nonaggregated column

    今天在Ubuntu下的部署项目,发现一些好好的列表页面发生 :Expression # of SELECT list is not in GROUP BY clause and contains no ...

  4. BarTender 2016如何导出模板为pdf文件?

    最近有小伙伴来问,BarTender 2016能不能导出模板为pdf文件?这个是可以的,之前针对BarTender 10.1就介绍过一种方法了.本文,小编再针对BarTender 2016给大家讲下如 ...

  5. 【ERROR】no matching function for call to 'std::basic_ifstream<char>::basic_ifstream

    错误记录:QT中使用 no matching function for call to 'std::basic_ifstream<char>::basic_ifstream(QString ...

  6. linux中,当执行rpm -e删除一个软件包时,都做了些什么事

    问题描述: 今天在通过rpm进行删除软件包时,出现了问题,就引发了我对于rpm包执行删除动作时的一些行为做了思考,之前找了很多的文章,后来想如果有debug日志信息,那么不就都清楚了吗 通过打印rpm ...

  7. oracle 子查询中null的问题(in 和 not in)

    这里的in后面的句子可以理解为or拼接,简单举例即 in (9566,9839,null)可以等价于mgr=9566 or mgr=9839 or mgr=null, not in (9566,983 ...

  8. python什么时候加self,什么时候不加self

    1.self是什么,一般都说指对象本身,这样说了没了用,说了后还是很难懂,因为这样说了后,仍然完全搞不清楚,什么时候变量前需要加self,什么时候不需要加self. 造成很多人,已经怕了self,不停 ...

  9. python 检测字符串编码类型是什么

    for index,value in enumerate(listvalue): adchar=chardet.detect(value) print adchar if adchar['encodi ...

  10. 使用PHP几种写99乘法表的方式

    首先按照规矩,还是先废话一番,对于刚学PHP的新手来讲,用php写九九乘法表无疑是非常经典的一道练习题. 但不要小看这道练习题,它对于逻辑的考验还是相当到位的. 也许有人会觉得,九九乘法表有什么难的, ...