Сообщение Re: Жизнь после Dispose (.net) от 13.07.2015 9:30
Изменено 02.05.2017 7:42 DDDX
Re: Жизнь после Dispose (.net)
Здравствуйте, ylem, Вы писали:
Y>Есть самодельный класс, реализующий IDisposable и с одним методом GetNext, который или возвращает кое-что, или говорит, что нечего возвращать.
Y>https://msdn.microsoft.com/en-us/library/system.idisposable(v=vs.110).aspx
Y>Оказалось удобным для использования сделать:
Y>1. после вызова Dispose() метод GetNext() говорит, что возвращать нечего (вместо того, чтобы кидать эксепшн https://msdn.microsoft.com/en-us/library/system.objectdisposedexception(v=vs.110).aspx)
Y>2. вызовы к GetNext() и Dispose() асинхронные, но внутри встают в очередь. Сделал так, что GetNext(), начавшиеся до Dispose, результат вернут, а начавшиеся после -- нет.
Y>На сколько 1 и 2 "правильно" и соответствует ожиданиям от IDisposable?
Тут уже написали — это неправильно. После Dispose можно вызвать только Dispose.
Для того что бы сделать правильный неблокирующий Dispose можно использовать счетчики. Но уже не ссылок, а внешних вызовов методов объекта.
Работающий пример можно посмотреть здесь.
Думаю, для осознания вышеобозначенного подхода потребуется не более двух стаканов.
Y>Есть самодельный класс, реализующий IDisposable и с одним методом GetNext, который или возвращает кое-что, или говорит, что нечего возвращать.
Y>https://msdn.microsoft.com/en-us/library/system.idisposable(v=vs.110).aspx
Y>Оказалось удобным для использования сделать:
Y>1. после вызова Dispose() метод GetNext() говорит, что возвращать нечего (вместо того, чтобы кидать эксепшн https://msdn.microsoft.com/en-us/library/system.objectdisposedexception(v=vs.110).aspx)
Y>2. вызовы к GetNext() и Dispose() асинхронные, но внутри встают в очередь. Сделал так, что GetNext(), начавшиеся до Dispose, результат вернут, а начавшиеся после -- нет.
Y>На сколько 1 и 2 "правильно" и соответствует ожиданиям от IDisposable?
Тут уже написали — это неправильно. После Dispose можно вызвать только Dispose.
Для того что бы сделать правильный неблокирующий Dispose можно использовать счетчики. Но уже не ссылок, а внешних вызовов методов объекта.
Работающий пример можно посмотреть здесь.
Кусок класса OleDbConnection | |
| |
Интерфейс Structure_IComponentLifeTerminator | |
| |
Исходный код структуры Structure_ComponentLifeManager. | |
| |
Думаю, для осознания вышеобозначенного подхода потребуется не более двух стаканов.
Re: Жизнь после Dispose (.net)
Здравствуйте, ylem, Вы писали:
Y>Есть самодельный класс, реализующий IDisposable и с одним методом GetNext, который или возвращает кое-что, или говорит, что нечего возвращать.
Y>https://msdn.microsoft.com/en-us/library/system.idisposable(v=vs.110).aspx
Y>Оказалось удобным для использования сделать:
Y>1. после вызова Dispose() метод GetNext() говорит, что возвращать нечего (вместо того, чтобы кидать эксепшн https://msdn.microsoft.com/en-us/library/system.objectdisposedexception(v=vs.110).aspx)
Y>2. вызовы к GetNext() и Dispose() асинхронные, но внутри встают в очередь. Сделал так, что GetNext(), начавшиеся до Dispose, результат вернут, а начавшиеся после -- нет.
Y>На сколько 1 и 2 "правильно" и соответствует ожиданиям от IDisposable?
Тут уже написали — это неправильно. После Dispose можно вызвать только Dispose.
Для того что бы сделать правильный неблокирующий Dispose можно использовать счетчики. Но уже не ссылок, а внешних вызовов методов объекта.
Работающий пример можно посмотреть здесь.
Думаю, для осознания вышеобозначенного подхода потребуется не более двух стаканов.
Y>Есть самодельный класс, реализующий IDisposable и с одним методом GetNext, который или возвращает кое-что, или говорит, что нечего возвращать.
Y>https://msdn.microsoft.com/en-us/library/system.idisposable(v=vs.110).aspx
Y>Оказалось удобным для использования сделать:
Y>1. после вызова Dispose() метод GetNext() говорит, что возвращать нечего (вместо того, чтобы кидать эксепшн https://msdn.microsoft.com/en-us/library/system.objectdisposedexception(v=vs.110).aspx)
Y>2. вызовы к GetNext() и Dispose() асинхронные, но внутри встают в очередь. Сделал так, что GetNext(), начавшиеся до Dispose, результат вернут, а начавшиеся после -- нет.
Y>На сколько 1 и 2 "правильно" и соответствует ожиданиям от IDisposable?
Тут уже написали — это неправильно. После Dispose можно вызвать только Dispose.
Для того что бы сделать правильный неблокирующий Dispose можно использовать счетчики. Но уже не ссылок, а внешних вызовов методов объекта.
Работающий пример можно посмотреть здесь.
Кусок класса OleDbConnection | |
| |
Интерфейс Structure_IComponentLifeTerminator | |
| |
Исходный код структуры Structure_ComponentLifeManager. | |
| |
Думаю, для осознания вышеобозначенного подхода потребуется не более двух стаканов.