i using c# on .net 3.5 (cannot use higher versions)
i have implementation of blocking queue implements producer-consumer pattern loosely based on http://element533.blogspot.com/2010/01/stoppable-blocking-queue-for-net.html
i have 4 files in same namespace:
- file0 contains main function , starts 2 threads func1 , func2 , instantiates object of above queue
- file1 has func1 splits video individual frames of images (bitmaps) , enqueues them above queue. if items added, signals has completed adding queue. acts producer
- file2 has func2 checks if there items available in queue , dequeues first element. acts consumer
- contains implementation of queue
important bits of code
func1
(int index = 0; index < numframes; index++) { bitmap oneframe = videoreader.readvideoframe(); imageprocessor.framequeue.enqueue(oneframe); oneframe.dispose(); }
func2
while (!imageprocessor.framequeue.iscompleted()) { using (bitmap image = imageprocessor.framequeue.dequeue()) { console.writeline("height: " + image.height); console.writeline("width: " + image.width); } }
whenever runs, func1 runs expected func2 throws different types of errors when tries access image.height. of errors have seen are
1)
an unhandled exception of type 'system.invalidoperationexception' occurred in system.drawing.dll additional information: object in use elsewhere.
2)
an unhandled exception of type 'system.nullreferenceexception' occurred additional information: object reference not set instance of object.
3)
an unhandled exception of type 'system.argumentexception' occurred in system.drawing.dll additional information: parameter not valid.
any guesses i'm doing wrong? can not use multi-threading bitmaps?
i have feeling issue in oneframe.dispose() in func1
try removing oneframe.dispose();
func1. being marked disposal right after being queued. looks func2 consuming disposable object , attempt dispose anyway when exits using
block.
edit: 1 thing keep in mind if func2 doesn't work way through objects created, you'll have undisposed objects hanging around. in producer/consumer model, consumer's responsibility take care of produced.
Comments
Post a Comment