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. ssh远程登录不上的处理

    最近ssh远程主机突然登录不上,提示如下: 后来咨询了一下云主机的客服,估计我们的主机时多次尝试密码错误被系统屏蔽IP了.于是问了一下同事,确实有同事最近密码错误多次尝试的问题. 于是按照客服给的方法 ...

  2. oracle sql语句实现累加、累减、累乘、累除

    在oracle开发过程中经常会遇到累加.减.乘.除的问题.下面对这个做个小的总结 ---创建测试表 CREATE TABLE TEST( PARENT_ID NUMBER, PART_ID NUMBE ...

  3. spring-boot项目建立

    使用idea来开发spring-boot项目,对于community版本的idea,由于没有spring-boot插件,所有对于开发spring-boot的web项目来说不是很方便,所以安装Ultim ...

  4. AES和RSA加解密的Python用法

    AES AES 是一种对称加密算法,用key对一段text加密,则用同一个key对密文解密, from Crypto import Random from Crypto.Hash import SHA ...

  5. 更改了react-redux 官方网站的todolist结构

    最近在学习胡子大哈的react小书,内容讲的由浅入深,很值得react,react-redux小白一读. 废话不多说直接上地址:http://huziketang.mangojuice.top/boo ...

  6. 虚拟机---vmmare15安装centos7.4

    第一步:下载centos7的镜像iso文件:http://isoredirect.centos.org/centos/7/isos/x86_64/CentOS-7-x86_64-Everything- ...

  7. 【数据库问题】sql server 获取MD5值结果不一致的问题 substring(sys.fn_sqlvarbasetostr(HashBytes('MD5','111111')),11,32)

    获取 111111 的MD5值 SELECT substring(sys.fn_sqlvarbasetostr(HashBytes(,) 执行结果:965eb72c92a549dd5a330112 但 ...

  8. 19款Windows实用软件推荐,满满的干货,总有一款是你必备的

    https://post.smzdm.com/p/745799/ 追加修改(2018-08-20 12:28:23):一些追加内容: 很多人都在吐槽为什么推荐Clover,这里我说明一下,就我了解到的 ...

  9. 读《javascript语法精粹》知识点总结

    昨天泡了大半天的读书馆,一口气看完了<javascript语法精粹>这本书,总体来说这本书还是写的不错,难怪那么多的推荐.<javascript语法精粹>主要是归纳与总结了ja ...

  10. zoj 3871

    貌似这道题某人已经扔给我一个多星期了(雾) 首先要知道这样一点:凸包的面积可以直接用线段的有向面积和求得. 自己口胡的证明:单纯一条线段自身的叉积就是到原点与这条线段构成三角形的面积吧,那么加加减减之 ...