首页 > 编程语言 > 详细

项目里线程池的用法

时间:2015-02-13 19:47:31      阅读:373      评论:0      收藏:0      [点我收藏+]
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();
                }
            }
        }

 

项目里线程池的用法

原文:http://www.cnblogs.com/405464904/p/4290743.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!