简体   繁体   English

在选项卡中预加载组合框

[英]Pre-load ComboBoxes in a Tab

I have a Windows Form application with a tab that contains many ComboBoxes.我有一个 Windows 表单应用程序,其选项卡包含许多组合框。 Every time I access this tab, I need to wait a few seconds for it to load.每次访问此选项卡时,我都需要等待几秒钟才能加载。 Is there a way to pre-load ComboBoxes while I am still using others tab of my program?有没有办法在我仍在使用程序的其他选项卡时预加载组合框?

private ObjectCache cache = MemoryCache.Default;

private void LoadComboBoxData() {
  var txtboxrecmedicacoes = new[] { txtboxrecmedicacoes1,
                                    txtboxrecmedicacoes2,
                                    txtboxrecmedicacoes3,
                                    txtboxrecmedicacoes4,
                                    txtboxrecmedicacoes5,
                                    txtboxrecmedicacoes6,
                                    txtboxrecmedicacoes7,
                                    txtboxrecmedicacoes8};
  foreach (ComboBox comboBox in txtboxrecmedicacoes)
  {
    string cacheKey = comboBox.Name;
    if (cache.Contains(cacheKey))
    {
      comboBox.DataSource = cache[cacheKey];
    }
    else{}
  }
}

This is just part your text of combobox, there is more.这只是您的 combobox 文本的一部分,还有更多。 And I use this method on form load.我在表单加载时使用此方法。 I add the data to ComboBox on load of the form, the problem is when I select the specific tab.我在加载表单时将数据添加到 ComboBox,问题是当我将 select 添加到特定选项卡时。 The code that I made didn't work.我编写的代码不起作用。 How can I pre-load all components of my tab?如何预加载选项卡的所有组件?

You can use a caching technique like memory caching to avoid loading the same data multiple times您可以使用像 memory 缓存这样的缓存技术来避免多次加载相同的数据

private BackgroundWorker worker = new BackgroundWorker();

private void Form1_Load(object sender, EventArgs e)
{
    worker.DoWork += Worker_DoWork;
    worker.RunWorkerCompleted += Worker_RunWorkerCompleted;
    worker.RunWorkerAsync();
}

private void Worker_DoWork(object sender, DoWorkEventArgs e)
{
    //load combo box data here
    var txtboxrecmedicacoes = new[] { txtboxrecmedicacoes1, txtboxrecmedicacoes2, txtboxrecmedicacoes3, txtboxrecmedicacoes4, txtboxrecmedicacoes5, txtboxrecmedicacoes6, txtboxrecmedicacoes7, txtboxrecmedicacoes8 };
    foreach (ComboBox comboBox in txtboxrecmedicacoes)
    {
        string cacheKey = comboBox.Name;
        if (!cache.Contains(cacheKey))
        {
            //load data from DB or other sources and add to cache
            var data = LoadData(comboBox.Name);
            cache.Add(cacheKey, data, DateTimeOffset.UtcNow.AddHours(1));
        }
    }
}

private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    //update UI with loaded data
    var txtboxrecmedicacoes = new[] { txtboxrecmedicacoes1, txtboxrecmedicacoes2, txtboxrecmedicacoes3, txtboxrecmedicacoes4, txtboxrecmedicacoes5, txtboxrecmedicacoes6, txtboxrecmedicacoes7, txtboxrecmedicacoes8 };
    foreach (ComboBox comboBox in txtboxrecmedicacoes)
    {
        string cacheKey = comboBox.Name;
        if (cache.Contains(cacheKey))
        {
            comboBox.DataSource = cache[cacheKey];
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM