wegelagerer
wegelagerer

Reputation: 3710

Query inside a thread

I have 3 comboboxes that are loaded with data from LINQ queries on page load. The problem is that queries contain so much data that it causes Internet Explorer to stop responding for a bit more than a minute.

As there are 3 queries my idea is to put them in 3 different threads, but the problem is at the end the only thing I get is the error saying: "Both DataSource and DataSourceID are defined on 'cbOrganizator'. Remove one definition."

cbOrganizator is a combobox.

Here is the code:

protected void Page_Load(object sender, EventArgs e)
{
    Bind();
}

public void Osobe()
    {
        PravosudnaAkademijaEntities db = new PravosudnaAkademijaEntities();

        var osoba = from o in db.osobas
                    orderby o.osoba_prezime
                    select new { o.osoba_id, imePrezime = o.osoba_prezime + " " + o.osoba_ime + " | " + o.tijelo.tijelo_naziv + " | " + o.radno_mjesto.rm_naziv_m };

        cbPolaznik.DataSource = osoba;
        cbPolaznik.DataTextField = "imePrezime";
        cbPolaznik.DataValueField = "osoba_id";
        cbPolaznik.DataBind();
        cbPolaznik.Items.Insert(0, " ");

        cbPredavac.DataSource = osoba;
        cbPredavac.DataTextField = "imePrezime";
        cbPredavac.DataValueField = "osoba_id";
        cbPredavac.DataBind();
        cbPredavac.Items.Insert(0, " ");

        cbAOM.DataSource = osoba;
        cbAOM.DataTextField = "imePrezime";
        cbAOM.DataValueField = "osoba_id";
        cbAOM.DataBind();
        cbAOM.Items.Insert(0, " ");
    }

    public void Tijela()
    {
        PravosudnaAkademijaEntities db = new PravosudnaAkademijaEntities();

        var tijelo = from t in db.tijeloes
                     orderby t.tijelo_naziv
                     select new { t.tijelo_id, sve = t.tijelo_naziv + " | " + t.mjesto.zupanija_drzava.zupanija_naziv };

        cbOrganizator.DataSource = tijelo;
        cbOrganizator.DataTextField = "sve";
        cbOrganizator.DataValueField = "tijelo_id";
        cbOrganizator.DataBind();
        cbOrganizator.Items.Insert(0, " ");
    }

    public void Bind()
    {
         Thread tOsobe = new Thread(Osobe);
         tOsobe.Start();
         Thread tTijela = new Thread(Tijela);
         tTijela.Start();
    }

I don't know what's wrong so any help would be appreciated. The primary idea is to separate queries into threads so if my approach is wrong please let me know.

Upvotes: 0

Views: 187

Answers (2)

David
David

Reputation: 51

if you do really want to do it with threads i would suggest performing the query on the threadpool. you can abstract your method so that it is called in delegate of the threadpool method
so i would replace bind with

ThreadPool.QueueUserWorkItem(new WaitCallback(Osobe));
ThreadPool.QueueUserWorkItem(new WaitCallback(Tijela));

change signature of Osobe, and Tijela to acccept a Object eg.public void Osobe(object a )

you will also need to marshal the call across the thread as i am not sure if webforms will accept if the binding is happening on another thread.

All said and done is still feel the ajax method is the best way forward.

Upvotes: 1

Joe Enos
Joe Enos

Reputation: 40383

You're starting threads but not giving them a chance to finish before the page is loaded. I don't know how that results in your particular error, but if your page loads before the thread is completed, then you definitely won't get results.

I really don't see how you'll be able to accomplish what you're trying to do without AJAX.

Upvotes: 1

Related Questions