object lifetime question

  • Thread starter Thread starter erdem
  • Start date Start date
E

erdem

hi again,

here is my problem again:

i have a typed dataset dsA;
an adapter adapterA;

here is structure

mydataset dsA;
odbcdataadapter adapterA;
.....
.....

private form_Load(object sender, System.EventArgs e)
{
Thread thr;
thr = new Thread(new ThreadStart(PreLoader));
thr.IsBackground = false;
thr.Start();
}

private void PreLoader()
{
adapterA.Fill(dsA);
....
....
...

//Here Thread Ends.
}

Also there are some Datagrids whose datasource is dsA.TableX

After program runs, it works normally for a few time then an
NullRefernce Exception is thrown.

i think, it is because, the created thread ended and we filled dsA in
scope of that thread so garbage collector collect memory area.
WHY this happens, or what can i do to correct this problem.

need help

Thanks
erdem
 
erdem said:
hi again,

here is my problem again:

i have a typed dataset dsA;
an adapter adapterA;

here is structure

mydataset dsA;
odbcdataadapter adapterA;
....
....

private form_Load(object sender, System.EventArgs e)
{
Thread thr;
thr = new Thread(new ThreadStart(PreLoader));
thr.IsBackground = false;
thr.Start();
}

private void PreLoader()
{
adapterA.Fill(dsA);
....
....
...

//Here Thread Ends.
}

Also there are some Datagrids whose datasource is dsA.TableX

After program runs, it works normally for a few time then an NullRefernce
Exception is thrown.

i think, it is because, the created thread ended and we filled dsA in
scope of that thread so garbage collector collect memory area.

No, that doesn't make any sense. It's more likely because PreLoader hasn't
run or hasn't finished when you try to DataBind the grids.

It's fine to fill the DataSet on a background thread, but you make sure that
the background thread has finished before you use the DataSet. An easy way
to do this is to keep a form-level reference to the background thread and
call Thread.Join to wait for PreLoader to finish just before you data bind
the grids.

David
 
David,

That was my idea as well, however what is than the sence of the thread.

Cor
 
Cor Ligthert said:
David,

That was my idea as well, however what is than the sence of the thread.

Cor

You can do extra work and maintain UI responsiveness between Thread.Start
and Thread.Join.

David
 
Back
Top