跟小D每日学口语

项目里线程池的用法

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

 

posted @ 2015-02-13 17:49  腐乳  阅读(982)  评论(0编辑  收藏  举报