zoukankan      html  css  js  c++  java
  • 项目里线程池的用法

    public void GenerateData(string dbFullName)
            {
                bool bSuccess = true;
    
                if (File.Exists(dbFullName))
                {
                    File.Delete(dbFullName);
                }
    
                System.Data.SQLite.SQLiteConnection.CreateFile(dbFullName);
                if (File.Exists(dbFullName))
                {
                    SQLiteHelper.connectionString = SQLiteHelper.GetConn(dbFullName);
    
                    bSuccess = SQLiteHelper.InitGenerateDB(new string[] { 
                        CommonGenerateData.CreateBuildingType, 
                        CommonGenerateData.CreateLocationBasicInfo, 
                        CommonGenerateData.CreateLocationOtherInfo, 
                        CommonGenerateData.CreateEleLocation, 
                        CommonGenerateData.CreateEleLocationPct, 
                        CommonGenerateData.CreateLocationAttach, 
                        CommonGenerateData.CreateElection });
    
                    using (SQLiteConnection sqliteConn = new SQLiteConnection(SQLiteHelper.connectionString))
                    {
                        sqliteConn.Open();
                        List<Action<SQLiteTransaction>> delegetlist = new List<Action<SQLiteTransaction>>() { 
                            CommonGenerateData.GenerateBuildingType, 
                            CommonGenerateData.GenerateLocationBasicInfo,
                            CommonGenerateData.GenerateLocationOtherInfo,
                            CommonGenerateData.GenerateEleLocation,
                            CommonGenerateData.GenerateEleLocationPct,
                            CommonGenerateData.GenerateLocationAttach,
                            CommonGenerateData.GenerateElection
                        };
    
                        WaitHandle[] waitHandles = new WaitHandle[]{
                            new AutoResetEvent(false),
                            new AutoResetEvent(false),
                            new AutoResetEvent(false),
                            new AutoResetEvent(false),
                            new AutoResetEvent(false),
                            new AutoResetEvent(false),
                            new AutoResetEvent(false)
                        };
    
                        using (SQLiteTransaction tran = sqliteConn.BeginTransaction())
                        {
                            for (int i = 0; i < delegetlist.Count; i++)
                            {
                                Action<SQLiteTransaction> deleget = delegetlist[i];
                                AutoResetEvent resetEvent = (AutoResetEvent)waitHandles[i];
                                ThreadPool.QueueUserWorkItem(stateInfo => {
                                    deleget(tran);
                                    resetEvent.Set();
                                }, waitHandles[i]);
                            }
                            WaitHandle.WaitAll(waitHandles);
                            tran.Commit();
                        }
                        sqliteConn.Close();
                    }
                }
            }
  • 相关阅读:
    iOS 绕过https证书验证 请求数据
    RN import ** from ** 用法
    蓝牙开发笔记
    golang笔记
    python3 md5
    python3 zip压缩
    nginx应用
    zipimport.ZipImportError: can't find module 'encodings'
    python3 模块安装列表
    cmd笔记
  • 原文地址:https://www.cnblogs.com/405464904/p/4290743.html
Copyright © 2011-2022 走看看