{"id":2155,"date":"2024-04-25T13:45:20","date_gmt":"2024-04-25T05:45:20","guid":{"rendered":"https:\/\/sanlangcode.com\/?p=2155"},"modified":"2024-04-25T13:45:20","modified_gmt":"2024-04-25T05:45:20","slug":"c-net%e5%ba%94%e7%94%a8%e6%93%8d%e4%bd%9csqlite%e4%ba%92%e6%96%a5%e5%90%8c%e6%ad%a5%e9%94%81%e8%a7%a3%e5%86%b3%e5%b9%b6%e5%8f%91%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/sanlangcode.com\/index.php\/2024\/04\/25\/c-net%e5%ba%94%e7%94%a8%e6%93%8d%e4%bd%9csqlite%e4%ba%92%e6%96%a5%e5%90%8c%e6%ad%a5%e9%94%81%e8%a7%a3%e5%86%b3%e5%b9%b6%e5%8f%91%e9%97%ae%e9%a2%98\/","title":{"rendered":"C#.NET\u5e94\u7528\u64cd\u4f5cSqlite\u4e92\u65a5\u540c\u6b65\u9501\u89e3\u51b3\u5e76\u53d1\u95ee\u9898"},"content":{"rendered":"\n<p>\u7f16\u7a0b\u4e2d\uff0c\u540c\u6b65\u662f\u4e00\u79cd\u673a\u5236\uff0c\u7528\u4e8e\u63a7\u5236\u591a\u4e2a\u7ebf\u7a0b\u6216\u4efb\u52a1\u4e4b\u95f4\u7684\u987a\u5e8f\u6267\u884c\u548c\u6570\u636e\u8bbf\u95ee\uff0c\u4ee5\u786e\u4fdd\u6570\u636e\u7684\u4e00\u81f4\u6027\u548c\u5b89\u5168\u6027\u3002\u5f53\u591a\u4e2a\u7ebf\u7a0b\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\u65f6\uff0c\u5982\u679c\u6ca1\u6709\u9002\u5f53\u7684\u540c\u6b65\u673a\u5236\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6570\u636e\u4e0d\u4e00\u81f4\u6216\u7ade\u6001\u6761\u4ef6\u7684\u95ee\u9898\u3002<\/p>\n\n\n\n<p>\u540c\u6b65\u7684\u57fa\u672c\u76ee\u7684\u662f\u786e\u4fdd\u5728\u5e76\u53d1\u6267\u884c\u7684\u591a\u4e2a\u64cd\u4f5c\u4e2d\uff0c\u6bcf\u4e2a\u64cd\u4f5c\u6309\u7167\u9884\u671f\u7684\u987a\u5e8f\u548c\u6b63\u786e\u7684\u65b9\u5f0f\u6267\u884c\uff0c\u4ece\u800c\u907f\u514d\u6f5c\u5728\u7684\u9519\u8bef\u548c\u4e0d\u786e\u5b9a\u6027\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u540c\u6b65\u7684\u51e0\u79cd\u5e38\u89c1\u65b9\u5f0f\uff1a<\/h3>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>\u4e92\u65a5\u9501\uff08Mutex\uff09<\/strong>\uff1a\u786e\u4fdd\u5728\u4efb\u4f55\u7ed9\u5b9a\u65f6\u523b\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u8bbf\u95ee\u67d0\u4e2a\u8d44\u6e90\u6216\u4e34\u754c\u533a\u3002<\/li><li><strong>\u4fe1\u53f7\u91cf\uff08Semaphore\uff09<\/strong>\uff1a\u5141\u8bb8\u591a\u4e2a\u7ebf\u7a0b\u8bbf\u95ee\u4e34\u754c\u533a\uff0c\u4f46\u6709\u4e00\u4e2a\u9650\u5236\u6570\u91cf\u7684\u8bb8\u53ef\u8bc1\u3002\u4f8b\u5982\uff0c\u4e00\u4e2a\u4fe1\u53f7\u91cf\u53ef\u4ee5\u8bbe\u7f6e\u4e3a\u5141\u8bb8\u6700\u591a\u4e09\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bbf\u95ee\u3002<\/li><li><strong>\u4e8b\u4ef6\uff08Event\uff09<\/strong>\uff1a\u7528\u4e8e\u7ebf\u7a0b\u95f4\u7684\u901a\u4fe1\u548c\u540c\u6b65\uff0c\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u901a\u77e5\u53e6\u4e00\u4e2a\u6216\u591a\u4e2a\u7ebf\u7a0b\u67d0\u4e2a\u4e8b\u4ef6\u5df2\u53d1\u751f\u3002<\/li><li><strong>\u4e92\u65a5\u91cf\uff08Mutex\uff09<\/strong>\uff1a\u4e0e\u4e92\u65a5\u9501\u7c7b\u4f3c\uff0c\u4f46\u66f4\u9002\u7528\u4e8e\u8de8\u8fdb\u7a0b\u540c\u6b65\u3002<\/li><li><strong>\u6761\u4ef6\u53d8\u91cf\uff08Condition Variable\uff09<\/strong>\uff1a\u5728\u67d0\u4e9b\u540c\u6b65\u573a\u666f\u4e2d\uff0c\u7ebf\u7a0b\u9700\u8981\u7b49\u5f85\u67d0\u4e2a\u6761\u4ef6\u6ee1\u8db3\u540e\u624d\u80fd\u7ee7\u7eed\u6267\u884c\uff0c\u6761\u4ef6\u53d8\u91cf\u63d0\u4f9b\u4e86\u8fd9\u79cd\u7b49\u5f85\u673a\u5236\u3002<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>using System.Threading;\nusing System.Threading.Tasks;\nusing Microsoft.Data.Sqlite;\n\npublic class SqliteDatabase\n{\n    private readonly string _connectionString;\n    private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);\n\n    public SqliteDatabase(string connectionString)\n    {\n        _connectionString = connectionString;\n    }\n\n    public async Task ExecuteNonQueryAsync(string query)\n    {\n        await _semaphore.WaitAsync();\n        try\n        {\n            using (var connection = new SqliteConnection(_connectionString))\n            {\n                await connection.OpenAsync();\n                var command = connection.CreateCommand();\n                command.CommandText = query;\n                await command.ExecuteNonQueryAsync();\n            }\n        }\n        finally\n        {\n            _semaphore.Release();\n        }\n    }\n\n    public async Task&lt;object> ExecuteScalarAsync(string query)\n    {\n        await _semaphore.WaitAsync();\n        try\n        {\n            using (var connection = new SqliteConnection(_connectionString))\n            {\n                await connection.OpenAsync();\n                var command = connection.CreateCommand();\n                command.CommandText = query;\n                return await command.ExecuteScalarAsync();\n            }\n        }\n        finally\n        {\n            _semaphore.Release();\n        }\n    }\n\n    public async Task&lt;SqliteDataReader> ExecuteReaderAsync(string query)\n    {\n        await _semaphore.WaitAsync();\n        try\n        {\n            var connection = new SqliteConnection(_connectionString);\n            await connection.OpenAsync();\n            var command = connection.CreateCommand();\n            command.CommandText = query;\n            return await command.ExecuteReaderAsync(CommandBehavior.CloseConnection);\n        }\n        finally\n        {\n            _semaphore.Release();\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p><code>SemaphoreSlim<\/code>\u6784\u9020\u51fd\u6570\u6709\u4e24\u4e2a\u91cd\u8f7d\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><code>SemaphoreSlim(int initialCount, int maxCount)<\/code><\/li><li><code>SemaphoreSlim(int initialCount)<\/code><\/li><\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u53c2\u6570\u89e3\u91ca\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li><code>initialCount<\/code>: \u521d\u59cb\u4fe1\u53f7\u91cf\u8ba1\u6570\u3002\u8fd9\u51b3\u5b9a\u4e86\u5728\u6ca1\u6709\u4efb\u4f55\u7ebf\u7a0b\u7b49\u5f85\u4fe1\u53f7\u91cf\u65f6\u53ef\u4ee5\u83b7\u53d6\u4fe1\u53f7\u91cf\u7684\u7ebf\u7a0b\u6570\u91cf\u3002<\/li><li><code>maxCount<\/code>: \u4fe1\u53f7\u91cf\u7684\u6700\u5927\u8ba1\u6570\u3002\u8fd9\u662f\u4fe1\u53f7\u91cf\u53ef\u4ee5\u589e\u52a0\u5230\u7684\u6700\u5927\u503c\u3002\u9ed8\u8ba4\u4e3a<code>Int32.MaxValue<\/code>\u3002<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u533a\u522b\uff1a<\/h3>\n\n\n\n<ol class=\"wp-block-list\"><li><strong><code>new SemaphoreSlim(1, 1)<\/code><\/strong>:<ul><li><code>initialCount<\/code>\u4e3a1\uff0c\u8868\u793a\u521d\u59cb\u65f6\u4fe1\u53f7\u91cf\u4e3a1\u3002<\/li><li><code>maxCount<\/code>\u4e3a1\uff0c\u8868\u793a\u4fe1\u53f7\u91cf\u7684\u6700\u5927\u503c\u4e5f\u662f1\u3002<\/li><\/ul>\u8fd9\u79cd\u8bbe\u7f6e\u521b\u5efa\u4e86\u4e00\u4e2a\u4e8c\u8fdb\u5236\u4fe1\u53f7\u91cf\uff0c\u4e5f\u5c31\u662f\u53ea\u5141\u8bb8\u4e00\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bbf\u95ee\u53d7\u4fe1\u53f7\u91cf\u4fdd\u62a4\u7684\u8d44\u6e90\u3002<\/li><li><strong><code>new SemaphoreSlim(1)<\/code><\/strong>:<ul><li><code>initialCount<\/code>\u4e3a1\uff0c\u8868\u793a\u521d\u59cb\u65f6\u4fe1\u53f7\u91cf\u4e3a1\u3002<\/li><li>\u7531\u4e8e\u672a\u63d0\u4f9b<code>maxCount<\/code>\u53c2\u6570\uff0c\u6240\u4ee5\u4f7f\u7528\u9ed8\u8ba4\u503c<code>Int32.MaxValue<\/code>\uff0c\u8868\u793a\u4fe1\u53f7\u91cf\u7684\u6700\u5927\u503c\u662f<code>Int32.MaxValue<\/code>\u3002<\/li><\/ul>\u8fd9\u79cd\u8bbe\u7f6e\u4e5f\u662f\u521b\u5efa\u4e00\u4e2a\u4e8c\u8fdb\u5236\u4fe1\u53f7\u91cf\uff0c\u53ea\u5141\u8bb8\u4e00\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bbf\u95ee\u53d7\u4fe1\u53f7\u91cf\u4fdd\u62a4\u7684\u8d44\u6e90\uff0c\u4f46\u6700\u5927\u503c\u662f<code>Int32.MaxValue<\/code>\uff0c\u5b9e\u9645\u4e0a\u6ca1\u6709\u9650\u5236\u3002<\/li><\/ol>\n\n\n\n<p>\u603b\u7ed3\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u5982\u679c\u4f60\u60f3\u660e\u786e\u5730\u9650\u5236\u4fe1\u53f7\u91cf\u7684\u6700\u5927\u503c\uff0c\u4f7f\u7528<code>new SemaphoreSlim(1, 1)<\/code>\u3002<\/li><li>\u5982\u679c\u4f60\u53ea\u5173\u5fc3\u4fe1\u53f7\u91cf\u662f\u5426\u88ab\u9501\u5b9a\uff0c\u800c\u4e0d\u5173\u5fc3\u6700\u5927\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>new SemaphoreSlim(1)<\/code>\u3002<\/li><\/ul>\n\n\n\n<p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5982\u679c\u4f60\u53ea\u9700\u8981\u4e8c\u8fdb\u5236\u4fe1\u53f7\u91cf\uff08\u53ea\u5141\u8bb8\u4e00\u4e2a\u7ebf\u7a0b\u8bbf\u95ee\u8d44\u6e90\uff09\uff0c\u90a3\u4e48\u8fd9\u4e24\u79cd\u65b9\u5f0f\u90fd\u662f\u7b49\u6548\u7684\u3002\u4f46\u662f\uff0c<code>new SemaphoreSlim(1, 1)<\/code>\u63d0\u4f9b\u4e86\u66f4\u660e\u786e\u7684\u8bed\u4e49\uff0c\u53ef\u4ee5\u5e2e\u52a9\u8bfb\u8005\u66f4\u597d\u5730\u7406\u89e3\u4f60\u7684\u610f\u56fe\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>\u8fd9\u610f\u5473\u7740\uff1a<\/p>\n\n\n\n<p><code><br><\/code><strong><code>new SemaphoreSlim(2, 1)<\/code> \u521b\u5efa\u4e86\u4e00\u4e2a\u4fe1\u53f7\u91cf\uff0c\u5176\u521d\u59cb\u8ba1\u6570\u4e3a2\uff0c\u6700\u5927\u8ba1\u6570\u4e3a1\u3002<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u521d\u59cb\u65f6\uff0c\u6709\u4e24\u4e2a\u8bb8\u53ef\u8bc1\u53ef\u4f9b\u4f7f\u7528\u3002<\/li><li>\u6700\u591a\u53ef\u4ee5\u6709\u4e00\u4e2a\u8bb8\u53ef\u8bc1\u3002<\/li><\/ul>\n\n\n\n<p>\u8fd9\u79cd\u8bbe\u7f6e\u4f3c\u4e4e\u6709\u4e9b\u4e0d\u4e00\u81f4\u3002\u901a\u5e38\uff0c<code>\u6700\u5927\u8ba1\u6570<\/code>\u5e94\u8be5\u5927\u4e8e\u6216\u7b49\u4e8e <code>\u521d\u59cb\u8ba1\u6570<\/code>\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6700\u5927\u8ba1\u6570\u5c0f\u4e8e\u521d\u59cb\u8ba1\u6570\uff0c\u8fd9\u6837\u7684\u8bbe\u7f6e\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u53ef\u80fd\u4f1a\u5bfc\u81f4\u95ee\u9898\u3002<\/p>\n\n\n\n<p>\u6b63\u786e\u7684\u65b9\u5f0f\u5e94\u8be5\u662f\u8bbe\u7f6e\u6700\u5927\u8ba1\u6570\u5927\u4e8e\u6216\u7b49\u4e8e\u521d\u59cb\u8ba1\u6570\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u63a7\u5236\u5e76\u53d1\u8bbf\u95ee\u3002\u5982\u679c\u9700\u8981\u4e24\u4e2a\u5e76\u53d1\u8bbf\u95ee\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u4e3a<code>new SemaphoreSlim(2, 2)<\/code>\u3002<\/p>\n\n\n\n<p><strong><code>new SemaphoreSlim(1, 2)<\/code> \u521b\u5efa\u4e86\u4e00\u4e2a\u4fe1\u53f7\u91cf\uff0c\u5176\u521d\u59cb\u8ba1\u6570\u4e3a1\uff0c\u6700\u5927\u8ba1\u6570\u4e3a2\u3002<\/strong><\/p>\n\n\n\n<p>\u8fd9\u610f\u5473\u7740\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u521d\u59cb\u65f6\uff0c\u6709\u4e00\u4e2a\u8bb8\u53ef\u8bc1\u53ef\u4f9b\u4f7f\u7528\u3002<\/li><li>\u6700\u591a\u53ef\u4ee5\u6709\u4e24\u4e2a\u8bb8\u53ef\u8bc1\u3002<\/li><\/ul>\n\n\n\n<p>\u8fd9\u79cd\u8bbe\u7f6e\u5141\u8bb8\u591a\u8fbe\u4e24\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bbf\u95ee\u53d7\u4fe1\u53f7\u91cf\u4fdd\u62a4\u7684\u8d44\u6e90\u3002\u5f53\u6709\u4e24\u4e2a\u7ebf\u7a0b\u83b7\u53d6\u8bb8\u53ef\u8bc1\u65f6\uff0c\u5176\u4ed6\u7ebf\u7a0b\u5c06\u88ab\u963b\u585e\uff0c\u76f4\u5230\u6709\u4e00\u4e2a\u6216\u4e24\u4e2a\u8bb8\u53ef\u8bc1\u88ab\u91ca\u653e\u4e3a\u6b62\u3002<\/p>\n\n\n\n<p>\u8fd9\u79cd\u4fe1\u53f7\u91cf\u8bbe\u7f6e\u9002\u7528\u4e8e\u90a3\u4e9b<strong>\u9700\u8981\u5141\u8bb8\u4e00\u5b9a\u6570\u91cf\u7684\u5e76\u53d1\u8bbf\u95ee\uff0c\u4f46\u4e0d\u662f\u65e0\u9650\u5236\u7684\u5e76\u53d1\u8bbf\u95ee\u7684\u573a\u666f<\/strong>\u3002<\/p>\n\n\n\n<p><strong>\u4e92\u65a5\u9501\uff08Mutex\uff09<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\nusing System.Threading;\n\nclass Program\n{\n    \/\/ \u521b\u5efa\u4e00\u4e2a\u4e92\u65a5\u9501\n    private static Mutex mutex = new Mutex();\n\n    static void Main(string&#91;] args)\n    {\n        \/\/ \u521b\u5efa\u4e24\u4e2a\u7ebf\u7a0b\u6765\u6a21\u62df\u5e76\u53d1\u8bbf\u95ee\n        Thread thread1 = new Thread(DoWork);\n        Thread thread2 = new Thread(DoWork);\n\n        \/\/ \u542f\u52a8\u7ebf\u7a0b\n        thread1.Start(\"Thread 1\");\n        thread2.Start(\"Thread 2\");\n\n        \/\/ \u7b49\u5f85\u4e24\u4e2a\u7ebf\u7a0b\u5b8c\u6210\n        thread1.Join();\n        thread2.Join();\n\n        Console.WriteLine(\"Main thread exiting...\");\n    }\n\n    static void DoWork(object threadName)\n    {\n        Console.WriteLine($\"{threadName} is waiting for the mutex.\");\n\n        \/\/ \u7b49\u5f85\u83b7\u53d6\u4e92\u65a5\u9501\n        mutex.WaitOne();\n\n        Console.WriteLine($\"{threadName} has acquired the mutex.\");\n\n        \/\/ \u6a21\u62df\u5de5\u4f5c\n        Thread.Sleep(2000);\n\n        Console.WriteLine($\"{threadName} is releasing the mutex.\");\n        \n        \/\/ \u91ca\u653e\u4e92\u65a5\u9501\n        mutex.ReleaseMutex();\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a<code>mutex<\/code>\u7684<code>Mutex<\/code>\u5b9e\u4f8b\u3002<\/li><li>\u6211\u4eec\u542f\u52a8\u4e86\u4e24\u4e2a\u7ebf\u7a0b\uff08<code>thread1<\/code>\u548c<code>thread2<\/code>\uff09\u6765\u6a21\u62df\u5e76\u53d1\u8bbf\u95ee\u3002<\/li><li>\u6bcf\u4e2a\u7ebf\u7a0b\u5728\u5c1d\u8bd5\u83b7\u53d6\u4e92\u65a5\u9501\u4e4b\u524d\u90fd\u4f1a\u7b49\u5f85\u3002<\/li><li>\u4e00\u65e6\u83b7\u5f97\u4e92\u65a5\u9501\uff0c\u7ebf\u7a0b\u4f1a\u8fdb\u884c\u4e00\u4e9b\u6a21\u62df\u7684\u5de5\u4f5c\u3002<\/li><li>\u5b8c\u6210\u5de5\u4f5c\u540e\uff0c\u7ebf\u7a0b\u4f1a\u91ca\u653e\u4e92\u65a5\u9501\u3002<\/li><\/ul>\n\n\n\n<p>\u8fd9\u6837\uff0c\u6211\u4eec\u5c31\u786e\u4fdd\u4e86\u5728\u4efb\u4f55\u7ed9\u5b9a\u65f6\u523b\uff0c\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u8bbf\u95ee\u53d7\u4e92\u65a5\u9501\u4fdd\u62a4\u7684\u8d44\u6e90\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7f16\u7a0b\u4e2d\uff0c\u540c\u6b65\u662f\u4e00\u79cd\u673a\u5236\uff0c\u7528\u4e8e\u63a7\u5236&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32,4],"tags":[],"class_list":["post-2155","post","type-post","status-publish","format-standard","hentry","category-c","category-4"],"_links":{"self":[{"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/posts\/2155","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/comments?post=2155"}],"version-history":[{"count":0,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/posts\/2155\/revisions"}],"wp:attachment":[{"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/media?parent=2155"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/categories?post=2155"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/tags?post=2155"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}