Beispiel #1
0
 public StartUpNodeInitializer([NotNull] IPlanetFactoryCreator planetFactoryCreator
                               , [NotNull] IConstants constants
                               , [NotNull] IArrayBackgroundWorker backgroundWorker)
 {
     mPlanetFactoryCreator = planetFactoryCreator;
     mConstants            = constants;
     mBackgroundWorker     = backgroundWorker;
 }
Beispiel #2
0
    public void Run(IReadOnlyList <QuadTreeLeaf> loadingNodes, IPlanetFactoryCreator planetFactoryCreator)
    {
        mErrorMessage = null;
        var processorCount = Environment.ProcessorCount;
        var leafsPerProc   = loadingNodes.Count / processorCount;

        mBackgroundWorkers = new BackgroundWorker[processorCount];
        //mIsWorking = true;
        for (int j = 0; j < processorCount; ++j)
        {
            var startIndex = j * leafsPerProc;
            var endIndex   = startIndex + leafsPerProc;
            if (endIndex >= loadingNodes.Count)
            {
                endIndex = loadingNodes.Count - 1;
            }
            BackgroundWorker worker = new BackgroundWorker();
            mBackgroundWorkers[j] = worker;
            var planetFactory = planetFactoryCreator.CreatePlanetFactory();
            worker.DoWork += (sender, args) =>
            {
                var totalLeafs = endIndex - startIndex;
                for (int i = startIndex; i < endIndex; ++i)
                {
                    if (loadingNodes[i].IsPlanetDataNull())
                    {
                        loadingNodes[i].SetPlanets(planetFactory.CreatePlanetsForSector());
                    }

                    var floatData = ((float)(i - startIndex) / totalLeafs) * 100f;

                    worker.ReportProgress((int)floatData);
                }
            };
            worker.WorkerReportsProgress = true;
            worker.ProgressChanged      += (sender, args) => { CallBackFromLoad(sender, args); };
            worker.RunWorkerAsync();
            worker.RunWorkerCompleted += WorkerOnRunWorkerCompleted;
        }
    }