c# - A generic asynchronously assignment -
c# - A generic asynchronously assignment -
i trying create method asynchorously assign in winforms programme using backgroundworker
.
void assign<t>(func<object> method, ref t obj) { var bw = new backgroundworker(); bw.dowork += (object sender, doworkeventargs e) => e.result = ((func<object>)(e.argument))(); bw.runworkercompleted += (object sender, runworkercompletedeventargs e) => obj = (t)e.result; // error here bw.runworkerasync(method); } assign(slowmethodreturnsfloat, ref afloatvariable); assign(slowmethodreturnsdataset, ref adatasetvar);
however, got error of:
"cannot utilize ref or out parameter within lambda expression"
any other way implement it? approach not using backgroundworker
?
consider pass lambda instead of ref
sec argument:
void assign<t>(func<object> method, action<t> objsetter) { var bw = new backgroundworker(); bw.dowork += (object sender, doworkeventargs e) => e.result = ((func<object>)(e.argument))(); bw.runworkercompleted += (object sender, runworkercompletedeventargs e) => objsetter((t)e.result); bw.runworkerasync(method); } assign(slowmethodreturnsfloat, ret => afloatvariable = ret); assign(slowmethodreturnsdataset, ret => adatasetvar = ret);
c# winforms asynchronous lambda
Comments
Post a Comment