How to wait until a service finishes opening a window before processing it

How to wait until a service finishes opening a window before processing it

I have code that opens a window using a service in my WPF C# application. I want to create a print-screen image file of that window once it is opened. The problem is that my code is creating an image file that has a print-screen of the area BEFORE the window is actually opened. I tried to create a delegate on the ViewOpened event, so that it waits until the window is actually opened before I generate the print screen. But I still get an image of the area BEFORE the window opens. How can I fix this?

I have code that opens a window using a service in my WPF C# application. I want to create a print-screen image file of that window once it is opened. The problem is that my code is creating an image file that has a print-screen of the area BEFORE the window is actually opened. I tried to create a delegate on the ViewOpened event, so that it waits until the window is actually opened before I generate the print screen. But I still get an image of the area BEFORE the window opens. How can I fix this?

Here is my code:

    private void M_printScreenButton_Click(object sender, EventArgs e)
    {
        var allViews = SystemConfigurationComponent.GetAllViews();
        foreach (var view in allViews)
        {
            // TODO: Add condition to select which views to process. Maybe add a boolean field to View table.
            if (string.Equals(view.Uri, @"gtcs\gcs\mmc_userpreferences", StringComparison.InvariantCultureIgnoreCase))
            {
                ViewsIntegrationService.OpenMimic(view.Uri, "", "", 2, ThisMimic.Context, null);

            ViewsIntegrationService.ViewOpened += delegate
            {
                using (Bitmap bitmap = new Bitmap(view.DefaultWidth, view.DefaultHeight))
                {
                    using (Graphics g = Graphics.FromImage(bitmap))
                    {
                        g.CopyFromScreen(new Point(view.DefaultLeft, view.DefaultTop), Point.Empty,
                            new Size() {Width = view.DefaultWidth, Height = view.DefaultHeight});
                    }
                    bitmap.Save(@"C:\\temp\\GTCS_PrintScreens\\test.jpg", ImageFormat.Jpeg);
                    //bitmap.Save(view.WindowTitle.Replace(" ", string.Empty).Replace("'", string.Empty) + ".jpg", ImageFormat.Jpeg);
                }

                //ViewsIntegrationService.CloseView(new AlstomGuiMimicImpl(m_mimicsManager, ThisMimic));
            };
        }
    }
}


Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

C/C++ vs. Rust: A developer’s perspective

In this post, you'll see the difference between Rust and C/C++ in a developer’s perspective

Variable Introduction in C#[Bangla]

LIKE | COMMENT | SHARE | SUBSCRIBE A variable is nothing but a name given to a storage area that our programs can manipulate. Each variable in C# has a speci...