void ProcessAnalysis(IEnumerable <ItemAnalysis> analysis, WorkUnit workUnit)
        {
            foreach (var a in analysis)
            {
                var item = _itemRepository.GetByName(a.ItemName).FirstOrDefault();
                if (item == null)
                {
                    workUnit.InternalNotFoundInDatabase.Add(a);
                    if (AutoAddNewItemsToDatabase)
                    {
                        item      = new Item();
                        item.Name = a.ItemName;
                        _itemRepository.Save(item);
                    }
                }

                if (SaveObservationsToDatabase && item != null)
                {
                    if (SaveObservation(a, item))
                    {
                        workUnit.InternalSavedToDatabase.Add(a);
                    }
                }
            }
        }
        WorkUnit ProcessImage(Bitmap screenShot, string id, int intId, bool startWork)
        {
            var workUnit = new WorkUnit(unit =>
            {
                var t = new Thread(o => Process(o as WorkUnit));
                t.Start(unit);
            });

            workUnit.ID     = id;
            workUnit.IntId  = intId;
            workUnit.Bitmap = screenShot;

            if (startWork)
            {
                workUnit.Start();
            }
            return(workUnit);
        }
        void Process(WorkUnit workUnit)
        {
            var ssName = GetScreenshotName();

            if (SaveScreenshots)
            {
                workUnit.Bitmap.Save(Path.Combine(ScreenshotDirectory, ssName + ".png"));
            }

            var marketScreen = ImageDecomposer.GetMarketScreen(workUnit.Bitmap, UIScale, workUnit.Bitmap.Width,
                                                               workUnit.Bitmap.Height);

            workUnit.MarketScreen = marketScreen;
            workUnit.InvokeDecompComplete();

            if (SaveDecomposedImages)
            {
                SaveImages(ssName, marketScreen);
            }

            if (SaveScreenshots || SaveDecomposedImages)
            {
                workUnit.InvokeImageSaveComplete();
            }

            List <ItemAnalysis> analysis = new List <ItemAnalysis>();

            foreach (var l in marketScreen.ItemListings)
            {
                var a = _imageAnalyzer.Analyze(l);
                if (!KeepIconBitmapWithAnalysis)
                {
                    a.Icon.Dispose();
                }
                analysis.Add(a);
            }

            workUnit.InternalItems.AddRange(analysis);
            workUnit.InvokeOcrComplete();

            foreach (var a in analysis)
            {
                if (ItemPasses(a))
                {
                    workUnit.InternalValidItems.Add(a);
                }
                else
                {
                    workUnit.InternalInvalidItems.Add(a);
                }
            }

            if (LogObservationsToFile)
            {
                LogObservation(workUnit.ValidItems, true);
                LogObservation(workUnit.InvalidItems, false);
            }

            if (AutoAddNewItemsToDatabase || SaveObservationsToDatabase)
            {
                ProcessAnalysis(workUnit.ValidItems, workUnit);
                workUnit.InvokeDatabaseOpsComplete();
            }

            workUnit.Bitmap.Dispose();
            workUnit.InvokeComplete();
        }