简体   繁体   中英

Pre-load ComboBoxes in a Tab

I have a Windows Form application with a tab that contains many ComboBoxes. 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. 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. 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

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];
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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