c# - cannot await object in the async call -
i have method called dosomething()
returns object
, time-consuming. use async/await let run in background while gui showing "waiting response..." message.
in calling method:
task taskdosomething = new task(dosomethingasync); taskdosomething.start(); taskdosomething.wait(); // continue program
my dosomethingasync
method:
private async void dosomethingasync() { object result = await dosomething(); }
but getting error:
cannot await [objecttype]
and [objecttype]
type of object have created myself. used object above not confuse reader.
please let me know doing wrong , set me free of scattered processing of backgroundworker
.
i think you've misunderstood how async
, await
works. if dosomething()
long-running method isn't designed asynchrony, want in different thread, e.g. starting separate task:
task<object> task = task.run<object>(dosomething);
note means dosomething
won't able interact ui, it'll on different thread.
you can await
task, if want - async method. it's hard give more concrete advice without knowing more context.
from comments, might have like:
private async void handlebuttonclick(object sender, eventargs e) { task<object> task = task.run<object>(dosomething); object result = await task; status.text = "i've done something"; // use result? }
Comments
Post a Comment