c# - How to setup multiple async methods into a queue? -
i picked async/await, , wrote series of async methods each perform database sql task , tied each 1 different button. gui doesn't locked when click button, figure that's awesome.
however, if click each 1 desired result second button wait first task finish, , on. however, async/await if click multiple buttons i'm not guaranteed finish first. can create unwanted race conditions cause sql queries fail.
is there class/object/keyword or can use instead of having each async method handle individual sql command instead adds queue , somehow queue processed serially when has been added it?
there many possible designs this. prefer tpl dataflow.
you can use actionblock
, post async
delegates processed sequentially:
actionblock<func<task>> _block = new actionblock<func<task>>(action => action()); block.post(async () => { await task.delay(1000); messagebox.show("bar"); });
another possible solution asynchronously wait on semphoreslim
set 1 before executing async
operation ensure single 1 can executed @ time:
semaphoreslim _semaphore = new semaphoreslim(1); _semaphore.waitasync() try { await task.delay(1000); messagebox.show("bar"); } { _semaphore.release(); }
Comments
Post a Comment