My event can be called even if I delete my class, since the thread must end. It is reported that is called between try / catch and syncContext.Post (). How bad is that?
It doesn't look so bad. Once you set Finished=true . he should complete the publication of the new delegate and then exit the loop. However, you may run into CPU caching issues with Finished . A CPU core running in a loop can read the cached Finished value after another thread has changed the value, so the loop may erroneously continue. To prevent this, you should create a private field that changes only through Interlocked , and a method that changes this field:
private int _running = 1; public void Stop() { Interlocked.Exchange(ref _running, 0); }
and the loop will work while( _running > 0) .
Is there a way to wrap this class so that the user should not forget to call dispose. Currently, if they do not work, the application waits for the thread to complete, which will never happen. Setting it to a background thread seems lazy.
There is really no general way to avoid the burden of customer disposition. But if this class should always be used, for example, by Winforms components, then you can write a constructor that accepts the parent component and subscribe to the Disposed event Disposed parent. Therefore, when a parent component (for example, a form) is placed, this object is also deleted:
public MyClass(Component parent) { parent.Disposed += (s,e) => this.Dispose(); }
(I would choose the finalizer as an option, because it does not say whether / when it will be launched.)
source share