c# - Async with await do not work. Why? -
when try run code below, perform synchronous operations. why?
i following warning ...
warning 1 async method lacks 'await' operators , run synchronously. consider using 'await' operator await non-blocking api calls, or 'await task.run(...)' cpu-bound work on background thread.
private async void btprocessa_click(object sender, eventargs e) { await processaa(); await processab(); } public async task processaa() { (int = 0; <= 100; i++) { pbprocessoa.value = i; thread.sleep(500); } } public async task processab() { (int = 0; <= 100; i++) { pbprocessob.value = i; thread.sleep(500); } }
async
not mean "run code on background thread". if want know more async
, have introductory blog post, msdn docs great, , there's full guide task-based asynchronous pattern.
if want simulation of i/o-bound (or event-based) operations, should use task.delay
instead of thread.sleep
:
public async task processaa() { (int = 0; <= 100; i++) { pbprocessoa.value = i; await task.delay(500); } }
if want simulate cpu-bound operations, should pushing them off background task via task.run
:
public async task processaa() { (int = 0; <= 100; i++) { pbprocessoa.value = i; await task.run(() => { thread.sleep(500); }); } }
Comments
Post a Comment