简体   繁体   English

如何延迟UI更新,直到计算完成?

[英]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; 
}
  1. Never do anything beside the actual initialization in constructor 除了在构造函数中进行实际的初始化外,请勿执行任何其他操作
  2. Use Loaded event to implement initial work 使用Loaded事件实施初始工作
  3. Avoid calculations and other CPU heavy work in UI thread. 避免在UI线程中进行计算和其他CPU繁重的工作。 Use the backgroung thread to do this. 使用backgroung线程执行此操作。 Use async/await 使用异步/等待

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.

相关问题 UI 冻结,直到该方法完成。 怎么能不冻? - The UI freezes until the method is finished. How can not freeze? 如何在进行数据库操作时更新XAML,然后在完成后再次更新? - How can I update my XAML while a database operation is ongoing and then update it again after it's finished? 格式化文本文件后,如何在解析完文件后更新文件? - Formatting a text file, how to update the file after I finished parsing it? 如何在不阻止UI线程的情况下等待所有任务完成? - How to wait until all tasks finished without blocking UI thread? 如何在C#中的UI线程上延迟运行代码? - How can I delay running code on the UI thread in C#? 如何让 foreach 循环暂停,直到我的协程完成? - How can I make a foreach loop pause until my Coroutine is finished? 如何强制该jquery等待click事件完成? - How can I force this jquery to wait until click event has finished? 文件下载到浏览器后如何修改页面? - How can I modify a page after a file is finished downloading to the browser? 我可以“阻塞”一个函数,直到终止线程完成吗? - Can I “block” a function until an asynch thread has finished? 如何强制更新MVVM中的UI? - How can I force update of the UI in MVVM?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM