using statement usage in C# -
i have doubts regarding using statement:
i have class called
myjob disposable. have property on myjob jobresults disposable.
my code:
using (myjob job = new myjob()) { //do something. formatresults(job.jobresults) } public string formatresuls(jobresuts results) { }
my first question is: in case after using block both myjob , myjob.results disposed or myjob , not myjob.results?
i performing parallel processing w.r.t tasks in c#:
tasks alltasks = list<tasks> try { foreach(task t in alltasks) { // each tasks makes use of idisposable object. t.start(); } task.waitall(alltasks); } catch(aggregateexecption aex) { /// how ensure idisposables disposed if exception happens in of tasks? }
my second q, in code above, proper way ensure dispose off objects correctly when handling exceptions in tasks?
sorry if questions naive or confusing, still trying learn , understand c#. guys!
are both myjob , myjob.results disposed or myjob , not myjob.results?
that subjective implementation of dispose
method. haven't seen in question, i'll assume aren't disposing result
property in myjob.dispose
, hence latter.
as myjob
wrapped in using
statement, , again assuming nothing result
property, disposed opposed results
, isn't wrapped in using
statement.
you decide myjob
, encapsulates result
property, responsible disposable of well. if decide so, can dispose results
in myjobs.dispose
.
what proper way ensure dispose off objects correctly when handling exceptions in tasks?
if delegate passed task
wrapped in using
statement, safe, since using
transform code try-finally
block, if exception occurs in using
block, finally
block still run yourobject.dispose
.
Comments
Post a Comment