SQLite multiple threads
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的更多相关文章
- 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 ...
- 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 ...
- caffe网络在多线程中无法使用GPU的解决方案 | cpp caffe net run in multiple threads
本文首发于个人博客https://kezunlin.me/post/8d877e63/,欢迎阅读! cpp caffe net run in multiple threads Guide set_mo ...
- bsxfun.h multiple threads backup
https://code.google.com/p/deep-learning-faces/source/browse/trunk/cuda_ut/include/bsxfun.h?r=7&s ...
- 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- ...
- 临界区代码 critical section Locks and critical sections in multiple threads
临界区 在同步的程序设计中,临界区段(Critical section)指的是一个访问共享资源(例如:共享设备或是共享存储器)的程序片段,而这些共享资源有无法同时被多个线程访问的特性. 当有线程进入临 ...
- PatentTips - Controlling TSC offsets for multiple cores and threads
BACKGROUND Many processors include a time stamp count (TSC) counter which is typically implemented a ...
- 【腾讯Bugly干货分享】微信iOS SQLite源码优化实践
本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/57b58022433221be01499480 作者:张三华 前言 随着微信iO ...
- SQLite源程序分析之sqlite3.c
/****************************************************************************** ** This file is an a ...
随机推荐
- Selenium IDE 基本概念
要学会Selenium不难,难的是首先你懂不懂测试.没有测试的基础知识,没有对测试理论的实践和认知,没有对测试领域的情感和钻研精神,学会了Selenium这个工具对事情也没有实际帮助. 我是一个技术思 ...
- 通用 正则表达式 C# (.NET)Regex 总结
[参考]C#正则表达式Regex类的用法 语法: 1. new System.Text.RegularExpressions.Regex("\\$\\d{1,2}\\}"). ...
- 《effective Go》读后记录:GO基础
一个在线的Go编译器 如果还没来得及安装Go环境,想体验一下Go语言,可以在Go在线编译器 上运行Go程序. 格式化 让所有人都遵循一样的编码风格是一种理想,现在Go语言通过gofmt程序,让机器来处 ...
- C#项目”XXXXX”针对的是”.NETFramework,Version=v4.7.1”但此计算机没有安装它
遇到这样一个问题:C#项目”XXXXX”针对的是”.NETFramework,Version=v4.7.1”但此计算机没有安装它 就是我在打开别人的项目,发现别人的项目.Net Framework的版 ...
- 【CF961G】Partitions 第二类斯特林数
[CF961G]Partitions 题意:给出n个物品,每个物品有一个权值$w_i$,定义一个集合$S$的权值为$W(S)=|S|\sum\limits_{x\in S} w_x$,定义一个划分的权 ...
- docker-compose & docker 镜像/加速
docker-compose: http://sanwen.net/a/nuwruoo.html docker加速: http://guide.daocloud.io/dcs/daocloud-915 ...
- 11.1 vue(2)
2018-11-1 19:41:00 2018年倒数第二个月! 越努力越幸运!!!永远不要高估自己! python视频块看完了!还有30天吧就结束了! 今天老师讲的vue 主要是看官网文档 贴上连接 ...
- K - Popular Cows
来源poj2186 Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N ...
- c++试题2
一.写出下列程序的运行结果(40 分) 1.for(i=1;i<5;i++); cout << “OK” << endl; 程序执行后的输出结果是: OK ___ ...
- Linux 下挂载新硬盘方法
Linux的硬盘识别: 一般使用”fdisk -l”命令可以列出系统中当前连接的硬盘 设备和分区信息.新硬盘没有分区信息,则只显示硬盘大小信息. 1.关闭服务器加上新硬盘 2.启动服务器,以r ...