admin管理员组

文章数量:1296457

I need to build an application that opens complex windows (windows that take about 1 minute for the UI thread to open them). Then, once they are open and loaded, start generating images of these windows into .jpg files.

So naturally, my code first opens all the windows in a foreach loop, and then the code takes the snapshots in a Tick event handler.

But the problem is that the code that starts taking the snapshots gets executed way before the windows are loaded. This is a job for code that plays with the UI thread, which I am not familiar with. Here is my code:

protected override void DoStartCapture()
{
    try
    {
        m_dispatcherTimers = new List<DispatcherTimer>();
        foreach (var mimicGroup in m_mimicGroupsToCapture)
        {
            foreach (var mimic in mimicGroup.Mimics)
            {
                OpenView(mimic.View, 2);
            }

            var dispatcherTimer =
                new DispatcherTimer
                {
                    Interval = TimeSpan.FromSeconds(Convert.ToInt32(mimicGroup.CaptureIntervalInSeconds)),
                    Tag = mimicGroup
                };
            dispatcherTimer.Tick += M_mainDispatcherTimer_Tick;
            m_dispatcherTimers.Add(dispatcherTimer);
            dispatcherTimer.Start();
        }

        m_openViews = ViewsIntegrationService.GetOpenViews();
    }
    catch (Exception ex)
    {
        Trace.TraceError("View Capture Service error. Exception: " + ex.Message);
        StopCapture();
        throw;
    }
}

When I put a breakpoint on the line that declares the dispatcherTimer, that line gets hit before I see the windows. The OpenView method was written by someone else.

How can I modify this code so that it waits for all the views to be fully loaded by the UI thread?

I need to build an application that opens complex windows (windows that take about 1 minute for the UI thread to open them). Then, once they are open and loaded, start generating images of these windows into .jpg files.

So naturally, my code first opens all the windows in a foreach loop, and then the code takes the snapshots in a Tick event handler.

But the problem is that the code that starts taking the snapshots gets executed way before the windows are loaded. This is a job for code that plays with the UI thread, which I am not familiar with. Here is my code:

protected override void DoStartCapture()
{
    try
    {
        m_dispatcherTimers = new List<DispatcherTimer>();
        foreach (var mimicGroup in m_mimicGroupsToCapture)
        {
            foreach (var mimic in mimicGroup.Mimics)
            {
                OpenView(mimic.View, 2);
            }

            var dispatcherTimer =
                new DispatcherTimer
                {
                    Interval = TimeSpan.FromSeconds(Convert.ToInt32(mimicGroup.CaptureIntervalInSeconds)),
                    Tag = mimicGroup
                };
            dispatcherTimer.Tick += M_mainDispatcherTimer_Tick;
            m_dispatcherTimers.Add(dispatcherTimer);
            dispatcherTimer.Start();
        }

        m_openViews = ViewsIntegrationService.GetOpenViews();
    }
    catch (Exception ex)
    {
        Trace.TraceError("View Capture Service error. Exception: " + ex.Message);
        StopCapture();
        throw;
    }
}

When I put a breakpoint on the line that declares the dispatcherTimer, that line gets hit before I see the windows. The OpenView method was written by someone else.

How can I modify this code so that it waits for all the views to be fully loaded by the UI thread?

Share Improve this question edited Feb 12 at 6:06 Ivan Petrov 4,9872 gold badges11 silver badges23 bronze badges asked Feb 12 at 5:40 RayRay 4,94712 gold badges53 silver badges99 bronze badges 2
  • Have you tried listening to the events of the windows, like the loaded event? – JonasH Commented Feb 12 at 7:50
  • Ditto on the "Loaded" events. – Gerry Schmitz Commented Feb 12 at 20:25
Add a comment  | 

1 Answer 1

Reset to default 1

Nest the capture method call in the ContentRendered event of each window?

private void MainWindow_ContentRendered(object sender, EventArgs e)
{
    DoStartCapture();
}

本文标签: