.net - Dispose a SemaphoreSlim while WaitAsync -


the documentation of semaphoreslim says 'dispose should used when other operations have completed'.

how should following class adjusted thread b can call dispose() while thread awaiting async(). async() must throw objectdisposedexception when dispose() called.

class {    semaphoreslim _sem = new semaphoreslim(0);    public task async() { return _sem.waitasync(); }    public void dispose() { _sem.dispose(); } } 

how should following class adjusted thread b can call dispose() while thread awaiting async()

it shouldn't. documentation quote says, if operations aren't completed, dispose() shouldn't called. if there's thread still awaiting waitasync() operation isn't completed, , therefore dispose() should not called.

from comment:

dispose called means of cancelling pending requests , releasing resources.

that's ill-advised way of cancelling request.

sure, if puts object invalid state could cause exceptions in other threads could result in them aborting , hence abandoning work.

but there's no guarantee should so, , there's chance coded not expect such exception @ such point , clean appropriately.

if want cancel, normal cancellation. create task thus:

public task async(cancellationtoken cancellationtoken) {   return _sem.waitasync(cancellationtoken); } 

and use cancellationtoken cancel.


Comments

Popular posts from this blog

php - Admin SDK -- get information about the group -

dns - How To Use Custom Nameserver On Free Cloudflare? -

Python Error - TypeError: input expected at most 1 arguments, got 3 -