项目里线程池的用法

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();
                }
            }
        }

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。