Exemplo n.º 1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Work started...");
            var threadParams = new WorkerParams();

            try
            {
                var thread = new Thread(Worker);
                thread.Start(threadParams);
                Console.Write("Wait for the thread to complete:");
                while (!threadParams.CompleteEvent.WaitOne(200))
                {
                    Console.Write(".");
                }
                Console.WriteLine();
                if (threadParams.ResultStatus == WorkerResult.Error)
                {
                    Console.WriteLine("Here is what happened: "
                                      + threadParams.Exception.Message);
                }
            }
            catch (Exception x) //This *will NOT* catch the exception from within the thread.
            {
                Console.WriteLine("Something went wrong: " + x.Message);
            }
            Console.WriteLine("Done.");
            Console.ReadLine();
        }
Exemplo n.º 2
0
        public virtual void DownloadChapterAsync(ISemaphore semaphore, IChapterRecord chapter, string outputFolder, IDownloadFormatProvider formatProvider)
        {
            if (_backgroundWorker.IsBusy)
            {
                throw new InvalidOperationException("Download is currently in progress.");
            }

            if (semaphore == null)
            {
                throw new ArgumentNullException("semaphore");
            }
            if (chapter == null)
            {
                throw new ArgumentNullException("chapter");
            }
            if (String.IsNullOrEmpty(outputFolder))
            {
                throw new ArgumentException("Output folder must not be null or empty.", "outputFolder");
            }
            if (formatProvider == null)
            {
                throw new ArgumentNullException("formatProvider");
            }

            var workerParams = new WorkerParams()
            {
                Chapter        = chapter,
                Semaphore      = semaphore,
                OutputFolder   = outputFolder,
                FormatProvider = formatProvider
            };

            _backgroundWorker.RunWorkerAsync(workerParams);
        }
Exemplo n.º 3
0
 public Worker(ILogger <Worker> logger,
               IHostApplicationLifetime appLifetime,
               IOptions <WorkerParams> options,
               SubscriptionWorker subscriptionWorker,
               AzureAdWorker azureAdWorker)
 {
     _logger             = logger;
     _appLifetime        = appLifetime;
     _subscriptionWorker = subscriptionWorker;
     _azureAdWorker      = azureAdWorker;
     _parms = options.Value;
 }
Exemplo n.º 4
0
        /// <summary>
        /// 拼接瓦片
        /// </summary>
        /// <param name="tilesBounds"></param>
        /// <param name="tilePath"></param>
        /// <param name="outPutFileName"></param>
        private void CombineTilesByTiffWriter(BackgroundWorker worker, DoWorkEventArgs e)
        {
            try
            {
                WorkerParams param          = e.Argument as WorkerParams;
                TilesBounds  tb             = param.TilesBound;
                string       tilePath       = param.TilePath;
                string       outPutFilePath = param.OutPutFilePath;

                int minBlockColIdx = tb.minCol / param.TiffTileCount;
                int maxBlockColIdx = tb.maxCol / param.TiffTileCount;
                int minBlockRowIdx = tb.minRow / param.TiffTileCount;
                int maxBlockRowIdx = tb.maxRow / param.TiffTileCount;

                int totalFileCount = (maxBlockColIdx - minBlockColIdx + 1) * (maxBlockRowIdx - minBlockRowIdx + 1);
                worker.ReportProgress(0, string.Format("生成的TIFF共有{0}个,总大小估计为:{1} G", totalFileCount, totalFileCount * 0.75));
                for (int i = minBlockColIdx; i <= maxBlockColIdx; i++)
                {
                    for (int j = minBlockRowIdx; j <= maxBlockRowIdx; j++)
                    {
                        if (worker.CancellationPending)
                        {
                            e.Cancel = true;
                            return;
                        }

                        TilesBounds tilesBounds = new TilesBounds();
                        tilesBounds.zoomLevel = tb.zoomLevel;
                        //修正瓦片边界,防止周围黑边
                        tilesBounds.minCol = Math.Max(i * param.TiffTileCount, tb.minCol);
                        tilesBounds.maxCol = Math.Min(i * param.TiffTileCount + param.TiffTileCount - 1, tb.maxCol);
                        tilesBounds.minRow = Math.Max(j * param.TiffTileCount, tb.minRow);
                        tilesBounds.maxRow = Math.Min(j * param.TiffTileCount + param.TiffTileCount - 1, tb.maxRow);

                        CombineSingleTiff(worker, tilesBounds, tilePath, outPutFilePath);
                    }
                }
            }
            catch (Exception ex)
            {
                worker.ReportProgress(0, string.Format("合并图片生成TIFF时出现异常!msg:{0}", ex.Message));
            }
        }
Exemplo n.º 5
0
        public async Task <List <workerDto> > GetWorkersAsync(WorkerParams workerParams)
        {
            var query =
                (from worker in _context.Workers

                 select new workerDto
            {
                WorkerId = worker.WorkerId,
                FullName = worker.FullName,
                Coordinates = worker.Coordinates
            }
                ).AsNoTracking()
                .AsQueryable();

            if (!string.IsNullOrEmpty(workerParams.FullName))
            {
                query = query.Where(d => d.FullName.Contains(workerParams.FullName));
            }


            return(await query.ToListAsync());
        }
Exemplo n.º 6
0
        static void Worker(object obj)
        {
            WorkerParams p = (WorkerParams)obj;

            try
            {
                for (int i = 0; i < 100; i++)
                {
                    Thread.Sleep(30);
                }
                int a = 0;
                int b = 5 / a;
                p.ResultStatus = WorkerResult.Completed;
                p.Result       = 42;
            }
            catch (Exception x)
            {
                Console.WriteLine("Thread: something went wrong.");
                p.Exception    = x;
                p.ResultStatus = WorkerResult.Error;
            }
            p.CompleteEvent.Set();
        }
Exemplo n.º 7
0
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            WorkerParams     param  = e.Argument as WorkerParams;

            if (param != null)
            {
                switch (param.Type)
                {
                case 1:
                    CombineTilesByGdal(worker, e);
                    break;

                case 2:
                    CombineTilesByTiffWriter(worker, e);
                    break;

                default:
                    DownLoadMissFiles(worker, e);
                    break;
                }
            }
        }
Exemplo n.º 8
0
 public void Generate()
 {
     WorkerParams pars = new WorkerParams();
     pars.messages = LogQueue;
     pars.EnvVars = Environment.GetEnvironmentVariables();
     m_worker.RunWorkerAsync(pars);
 }
Exemplo n.º 9
0
        public virtual void DownloadChapterAsync(ISemaphore semaphore, IChapterRecord chapter, string outputFolder, IDownloadFormatProvider formatProvider)
        {
            if (_backgroundWorker.IsBusy)
                throw new InvalidOperationException("Download is currently in progress.");

            if (semaphore == null)
                throw new ArgumentNullException("semaphore");
            if (chapter == null)
                throw new ArgumentNullException("chapter");
            if (String.IsNullOrEmpty(outputFolder))
                throw new ArgumentException("Output folder must not be null or empty.", "outputFolder");
            if (formatProvider == null)
                throw new ArgumentNullException("formatProvider");

            var workerParams = new WorkerParams()
            {
                Chapter = chapter,
                Semaphore = semaphore,
                OutputFolder = outputFolder,
                FormatProvider = formatProvider
            };

            _backgroundWorker.RunWorkerAsync(workerParams);
        }
Exemplo n.º 10
0
        public async Task <ActionResult <List <workerDto> > > GetWorkers([FromQuery] WorkerParams workerParams)
        {
            var workers = await _workerRepository.GetWorkersAsync(workerParams);

            return(Ok(workers));
        }
Exemplo n.º 11
0
        public virtual void DownloadChapterAsync(ISemaphore semaphore, ChapterRecord chapter, FileInfo file)
        {
            if (_backgroundWorker.IsBusy)
                throw new InvalidOperationException("Download is currently in progress.");

            if (semaphore == null)
                throw new ArgumentNullException("semaphore");
            if (chapter == null)
                throw new ArgumentNullException("chapter");
            if (file == null)
                throw new ArgumentNullException("file");

            var workerParams = new WorkerParams()
                    {
                        Chapter = chapter,
                        IsFile = true,
                        File = file,
                        Semaphore = semaphore
                    };

            _backgroundWorker.RunWorkerAsync(workerParams);
        }
Exemplo n.º 12
0
        public virtual void DownloadChapterAsync(ISemaphore semaphore, ChapterRecord chapter, DirectoryInfo directory, bool createDir = true)
        {
            if (_backgroundWorker.IsBusy)
                throw new InvalidOperationException("Download is currently in progress.");

            if (semaphore == null)
                throw new ArgumentNullException("semaphore");
            if (chapter == null)
                throw new ArgumentNullException("chapter");
            if (directory == null)
                throw new ArgumentNullException("directory");
            if (!createDir && !directory.Exists)
                throw new ArgumentException("Specified directory does not exists.", "directory");

            var workerParams = new WorkerParams()
                    {
                        Chapter = chapter,
                        IsFile = false,
                        Directory = directory,
                        CreateDirectory = createDir,
                        Semaphore = semaphore
                    };

            _backgroundWorker.RunWorkerAsync(workerParams);
        }
Exemplo n.º 13
0
        private void DownLoadMissFiles(BackgroundWorker worker, DoWorkEventArgs e)
        {
            try
            {
                WorkerParams param       = e.Argument as WorkerParams;
                TilesBounds  tilesBounds = param.TilesBound;
                int          i           = 0;
                for (int col = tilesBounds.minCol; col <= tilesBounds.maxCol; col++)
                {
                    worker.ReportProgress(col, string.Format("开始下载{0}目录图片!", col));
                    string colDir = Path.Combine(textTilePath.Text, numZoomLevel.Text, col.ToString());
                    if (!Directory.Exists(colDir))
                    {
                        Directory.CreateDirectory(colDir);
                    }
                    for (int row = tilesBounds.minRow; row <= tilesBounds.maxRow; row++)
                    {
                        if (worker.CancellationPending)
                        {
                            e.Cancel = true;
                            return;
                        }
                        string sourceFileName = Path.Combine(colDir, row.ToString()) + fileExt;
                        if (!File.Exists(sourceFileName))
                        {
                            string fileUrl = string.Empty;
                            if (param.MapType == 0)
                            {
                                fileUrl = string.Format("http://khm{0}.google.com/kh/v={5}&hl=en&x={1}&y={2}&z={3}&s={4}", i, col, row, tilesBounds.zoomLevel,
                                                        SourceTail.Substring(0, rng.Next(SourceTail.Length)), param.GoogleRasterV);
                            }
                            else if (param.MapType == 1)
                            {
                                fileUrl = string.Format("http://mt{0}.google.cn/vt/lyrs=t@132&x={1}&y={2}&z={3}&s={4}", i, col, row, tilesBounds.zoomLevel,
                                                        SourceTail.Substring(0, rng.Next(SourceTail.Length)));
                            }
                            else if (param.MapType == 2)
                            {
                                fileUrl = string.Format("http://mt{0}.google.cn/vt/lyrs=m@292000000&hl=zh-CN&gl=cn&x={1}&y={2}&z={3}&s={4}", i, col, row, tilesBounds.zoomLevel,
                                                        SourceTail.Substring(0, rng.Next(SourceTail.Length)));
                            }
                            else if (param.MapType == 3)
                            {
                                fileUrl = string.Format("http://10.55.13.160/ArcGIS/rest/services/water_baseMap/MapServer/tile/{3}/{2}/{1}", i, col, row, tilesBounds.zoomLevel,
                                                        SourceTail.Substring(0, rng.Next(SourceTail.Length)));
                            }
                            try
                            {
                                WebClient wc = new WebClient();
                                wc.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:21.0) Gecko/20130109 Firefox/21.0");
                                if (param.DownloadUseProxy)
                                {
                                    wc.Proxy = new WebProxy("127.0.0.1", 41080);
                                }
                                wc.DownloadFile(fileUrl, sourceFileName);
                                Thread.Sleep(10);
                            }
                            catch (Exception ex)
                            {
                                worker.ReportProgress(col, string.Format("下载图片{0}_{1}时出现错误!msg:{2} ,url:{3}", col, row, ex.Message, fileUrl));
                            }

                            i++;
                            i = i % 4;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                worker.ReportProgress(0, string.Format("下载图片时出现异常!msg:{0}", ex.Message));
            }
        }