Beispiel #1
0
        public void RectangleFactory()
        {
            bool istaken = false;
            WeakReference <MyRectangle> weakRectangle = null;

            do
            {
                istaken = _repository.TryDequeue(out weakRectangle);
            } while (!istaken);

            istaken = weakRectangle.TryGetTarget(out MyRectangle rectangle);
            if (!istaken)
            {
                rectangle = new MyRectangle(Statics.RandomColor(), new Point(Statics.generateRandomNumberBetween(4, _rectMaxWidth / 4), Statics.generateRandomNumberBetween(4, _rectMaxWidth / 4)), Statics.generateRandomNumberBetween(10, _rectMaxWidth), Statics.generateRandomNumberBetween(10, _rectMaxHeight));
            }


            PictureBox concreteRectangle = null;

            istaken = false;
            do
            {
                istaken = _pictureBoxPool.TryDequeue(out concreteRectangle);
            }while (!istaken);
            concreteRectangle.Location = rectangle.Location;
            concreteRectangle.Width    = rectangle.Width;
            concreteRectangle.Height   = rectangle.Height;

            Bitmap   rectangleImage = new Bitmap(rectangle.Width, rectangle.Height);
            Graphics graphics       = Graphics.FromImage(rectangleImage);

            graphics.FillRectangle(new SolidBrush(rectangle.Color), new RectangleF(new Point(1, 1), new Size(rectangle.Width - 2, rectangle.Height - 2)));
            graphics.Dispose();

            concreteRectangle.drawBorder(1, Color.Black);
            concreteRectangle.Image = rectangleImage;

            concreteRectangle.Visible = true;


            _pictureBoxPool.Enqueue(concreteRectangle);
            weakRectangle.SetTarget(rectangle);
            _repository.Enqueue(weakRectangle);
        }
Beispiel #2
0
        private async Task FillRepositoryInternal()
        {
            Task tsk = Task.Run(async() =>
            {
                for (int i = 0; i < 100000; i++)
                {
                    await Task.Run(() =>
                    {
                        MyRectangle rectangle = new MyRectangle(Statics.RandomColor(), new Point(Statics.generateRandomNumberBetween(4, this.pnlMainView.Width / 4), Statics.generateRandomNumberBetween(4, this.pnlMainView.Height / 4)), Statics.generateRandomNumberBetween(10, this.pnlMainView.Width), Statics.generateRandomNumberBetween(10, this.pnlMainView.Height));
                        _repository.Enqueue(new WeakReference <MyRectangle>(rectangle));

                        string jrect = $"Rectangle N# {i+1} added, \n {JsonConvert.SerializeObject(rectangle)}";

                        Action a = () => { lblInfo.Text = jrect; };
                        if (lblInfo.InvokeRequired)
                        {
                            try
                            {
                                lblInfo.BeginInvoke(a);
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show($"{ex.GetType().Name}\n\n{ex.Message}");
                            }
                        }
                        else
                        {
                            a();
                        }
                    });
                }
            });
            await tsk;

            if (tsk.IsCompleted)
            {
                await FillpictureBoxPool();
            }
        }