views:

45

answers:

1

I have a question about hibernate. I use different controls in my application (treeview, combobox, ...). I get the content for these controls through nhibernate. The problem is, that it takes a lot of time to get the data. Drung this time the form is frozen.

I want to load the data in another thread. But i don't know where to put that thread. I'm new at hibernate, maybe you have more experience about that.

+2  A: 

This isn't really an NHibernate problem, but rather a .NET Windows Forms threading one. Anyways, on a Forms environment, the easiest way to load all the NHibernate on a background thread would be to use the BackgroundWorker component.

private void LoadData(object sender, EventArgs e)
{
    // This event fires whatever's in DoWork() on a separate thread.
    backgroundWorker1.RunWorkerAsync();

    // Things to do asynchronous operation.
    timer1.Start();
}

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    // NHibernate loading goes here...
    var employees = Session.CreateCriteria<Employee>();
    combobox1.DataSource = employees;
}
Rafael Belliard
Calling UI functions from a background thread is dangerous. So you should probably wait to assign the combobox1.DataSource till the RunWorkerCompleted event. DoWork is called in the context of the background thread. RunWorkerCompleted will be called on the originating UI thread.
David Lynch
That is correct. I just wanted to illustrate what you could do in a background thread without being overly verbose about the usage of the component. :)
Rafael Belliard
Thx for your help!