Load Combobox Datasource using asynchronous method

Asked

Viewed 185 times

1

I have a combo box of cities that fill it every time I start my form. I wish someone could help me create an asynchronous method using SqlDataAdapter to fill this combo.

  • 1

    Some details about what you need and the current implementation are missing. First of all: why you need to fill out the combo asynchronously?

  • As far as I know about asynchronous methods, they release the main thread and thus improve performance in some cases, right? From what I understand, in this case could release the initialization of the form, while filling the combo

  • If the intention is just not to lock the main thread, your thinking is correct. But does this load take so long to lock the main thread? Are there so many logs? Wouldn’t it be the case to review the way you search the data in the database? See, maybe you’re looking for a solution to a problem that doesn’t even exist, or maybe the problem is another.

  • In this case, I’m listing the cities for a recipient registry of a management system, and I’m listing all the cities in the country. I would like to try using async to, as I said, not lock the main thread

1 answer

1


No more details about the current implementation and the real need to fill out the combo asynchronously is hard to try to help you. If you give more details, I can adapt the answer to your case.

I imagine that’s what you’re looking for:

private void QualquerForm_Shown(object sender, EventArgs e)
{         
    await PreencheCombo();
}

private Task PreencheCombo()
{
    return Task.Factory.StartNew(() =>
    {
        Invoke(new MethodInvoker(() => cbCidades.DataSource = Cidades.FindAll()));
    });
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.