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
1 Answer
Reset to default 1Nest the capture method call in the ContentRendered
event of each window?
private void MainWindow_ContentRendered(object sender, EventArgs e)
{
DoStartCapture();
}
本文标签:
版权声明:本文标题:c# - How can a WPF application wait for windows to be fully openned by the UI thread before processing them? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741619866a2388745.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论