[英]how can i delay UI update until after calculations are finished?
my c# winforms program has one tabcontrol with a few tabs displaying listviews, labels, buttons etc. the tabpages are not shown on load. 我的c#winforms程序有一个tabcontrol,其中有几个选项卡显示列表视图,标签,按钮等。这些选项卡在加载时不显示。 the code simplified looks like this: 简化的代码如下所示:
public main()
{
InitializeComponent();
removeTabPages(); //removes all but one; deleting this line doesn't change anything
main_tabcontrol.SelectedIndex = 0; //doesn't change anything no matter where i put it
loadData();
doSomeCalculations();
addTabPages();
main_tabcontrol.SelectedIndex = 2; //same issue if i pick any other tab here
}
private void Tabs_SelectedIndexChanged(object sender, EventArgs e)
{
UpdateDataInTab();
}
this loads some data, does some calculations and then switches to tabpage 2. i would expect to see the data processed by the loadData() and doSomeCalculations() functions displayed. 这将加载一些数据,执行一些计算,然后切换到选项卡页2。我希望看到显示的loadData()和doSomeCalculations()函数处理的数据。 instead it displays the default values (mostly nothing) until i switch to another tab and then back. 相反,它显示默认值(几乎没有显示),直到我切换到另一个选项卡,然后再返回。 that also verifies Tabs_SelectedIndexChanged() works as intended. 还可以验证Tabs_SelectedIndexChanged()是否按预期工作。
i'd like to understand why this happens and how i can make it work as planned. 我想了解为什么会发生这种情况,以及如何使它按计划工作。
running loadData() and doSomeCalculations() as async tasks, and awaiting them, does solve this, but it opens so many other problems that i'd like to avoid it (i don't need this async). 作为异步任务运行loadData()和doSomeCalculations()并等待它们确实可以解决此问题,但是它带来了许多其他问题,我想避免它(我不需要此异步)。 since my issue is the exact opposite (i need the code to run synchronous), this shouldn't be the solution anyways. 由于我的问题正好相反(我需要代码同步运行),因此无论如何都不应该是解决方案。
It is because you have it in your constructor, the best place to put it is on the load: 这是因为您在构造函数中拥有它,放置它的最佳位置是负载:
private void main_Load(object sender, EventArgs e)
{
main_tabcontrol.SelectedIndex = 0;
loadData();
doSomeCalculations();
main_tabcontrol.SelectedIndex = 2;
}
To use Load Event double click empty space on the form or use Properties Explorer and switch to events. 若要使用“加载事件”,请双击表单上的空白区域,或使用“属性资源管理器”并切换到事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.