Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

The calling thread cannot access this object because a different thread owns it.

$
0
0

I want use Mutli-Thread save all UserControl as image at the same time want to show animation view on the screen

but have a error is The calling thread cannot access this object because a different thread owns it.

This is my code:

private void OnScreenShotAllCommandExecuted(object parameter)
        {
string filePath = @"D:\ScreenShot\";
            FormattedText text = new FormattedText("Screen Name:", CultureInfo.GetCultureInfo("en-us"), FlowDirection.LeftToRight, new Typeface("Segeo UI"), 24, Brushes.Gray);
            IList<UserControl> userControls = LayoutRoot.Children.OfType<UserControl>().ToList();
            List<Thread> tasks = new List<Thread>();
            AnimationView animationView = new AnimationView();
           animationView.Show();

            foreach (UserControl userControl in userControls)
            {
                string viewName = userControl.Name;
                string fileName = userControl.Name + ".png";
                if (viewName != null)
                {
                    Thread thread = new Thread(() =>
                    {
                        Thread.Sleep(1000);
                        SaveView(userControl, fileName, text, filePath);
                         ThreadHasFinished();
                    });
                    tasks.Add(thread);
                    thread.SetApartmentState(ApartmentState.STA);
                    thread.Start();
                }
            }
        }

        private void ThreadHasFinished()
        {
            Interlocked.Increment(ref finishedControls);
          
            Dispatcher.BeginInvoke((Action)(() =>
            {
                animationView.Close();
            }));
        }
 public static void SaveView(UserControl userControl, string fileName, FormattedText text, string destFolder)
        {
            Rect bounds = VisualTreeHelper.GetDescendantBounds(userControl);

            int width = (int)view.RenderSize.Width;
            int height = (int)view.RenderSize.Height;


           RenderTargetBitmap rtb = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);

            DrawingVisual dv = new DrawingVisual();
            using (DrawingContext ctx = dv.RenderOpen())
            {
                VisualBrush vb = new VisualBrush(userControl);
                ctx.DrawRectangle(vb, new Pen(Brushes.Blue, 2), new Rect(new Point(0, 100), size));
                ctx.DrawText(text, new Point(0, 20));
                ctx.Close();
            }
            rtb.Render(dv);
            PngBitmapEncoder png = new PngBitmapEncoder();
            png.Frames.Add(BitmapFrame.Create(rtb));

            using (Stream fileStream = new FileStream(string.Format("{0}\\{1}.png", destFolder, fileName), FileMode.Create))
            {
                png.Save(fileStream);
            }        }

The error "The calling thread cannot access this object because a different thread owns it." on this line "Rect bounds = VisualTreeHelper.GetDescendantBounds(userControl);"

Waiting for your help. Thank you very much.


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>