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));
            };
        }
    }
}


c-sharp Windows-Presentation-Foundation

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

C. C++. C#. 차이점 알려드림. 5분 순삭. Explain C.C++.C#. Like I'm Five

C. C++. C#. 차이점 알려드림. 5분 순삭. Explain C.C++.C#. Like I'm Five. 아시죠? C는 C++.C# 줄인말이 아니라는거?

Learn how to perform CRUD operations using C Sharp

QuickStart: CRUD Operations in C Sharp. Learn how to perform CRUD operations using C Sharp for MongoDB databases.

Loops in C++ | For, While, and Do While Loops in C++

In this Video We are going to see how to use Loops in C++. We will see How to use For, While, and Do While Loops in C++.

Using isdigit() in C/C++

In this article, we'll take a look at using the isdigit() function in C/C++. This is a very simple way to check if any value is a digit or not. Let's look

C++ Tutorial for Beginners 19 - C++ Structures | Struct declaration | C++ Data Structures

Welcome to this course on C++ Tutorial for Beginners. In this video we will see How to use C++ Structures.