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?