object lifetime

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

erdem

hi,


i have problem about object references,,

lets say: i have a typed dataset (name: "dsall")
it has datatables table1 , table2
there are dataadapters which fills these and there are also
datagrids,comboboxes etc which are binded to this dataset

then i write this code to FORM_LOAD

//create thread in order not to wait getting process

private void formload(.......,.....)
{

Thread thr =new Thread(new ThreadStart(xx));
thr.isBackGround = true;
thr.Start();

//other things
}

private void xx()
{
adapter1.Fill(dsAll.Table1);
adapter1.Fill(dsAll.Table2);
//etc etc..
}

here is problem
when this code is executed, Null Reference exception is thrown

as i understand adapter1 creates datatable in thread scope and because
thread is ended, garbage collector collects datatable so i get null
reference exception, i say this because if i use this form there is no
error.

private void formload(.......,.....)
{

//dont use thread
//Thread thr =new Thread(new ThreadStart(xx));
//thr.Start();
//call directly function
xx();

//other things
}

but i need to fill dataset with thread

Thanks in advance

Erdem...
 
Erdem,

An object lives as long as there is an reference to is.

In my opinion is propably your problem that when the dataset is needed in
thread A, it is still busy to be created in thread B.

Just my thought,

Cor
 
Cor said:
Erdem,

An object lives as long as there is an reference to is.

In my opinion is propably your problem that when the dataset is needed in
thread A, it is still busy to be created in thread B.

Just my thought,

Cor
Hi Cor,

thanks for your advice,

i thought as you thought and tried to give datasource of controls at
thread b so that thread A will no longer need it, while thread B is
processing dataset. but there appeared another problem,
you cant give datasource of control in another thread (other than main
thread i think)
so i saw i should give datasource within main thread

so i found new solution
i use begin invoke method so that controls datasource is given within
main thread and does processing at background...

thank you

Erdem
 
Cor... If I may clarify. An object is eligible for garbage collection
when it is unreachable. It is a feature of C# that two objects with
references to each other (circular references) may be eligible for
garbage collection if both objects are unreachable.

Regards,
Jeff
 
Back
Top