BackgroundWorker DisconnectedContext

i have the folling code:

public static Emgu.CV.Capture _capture;
public static DispatcherTimer _timer;
_timer = new DispatcherTimer();
_timer.Interval = _settings.camera_interval;
_timer.Tick += ProcessFrame;
BacgroundWorker _bw = new BackgroundWorker
    WorkerReportsProgress = true,
    WorkerSupportsCancellation = true

_bw.DoWork += (s, e) =>
    // Initialize the device in background
    _capture = new Capture();

_bw.RunWorkerCompleted += (s, e) =>
    Brightness = _capture

    Contrast = _capture

    // Get images from camera

 public override void CleanUp()
     if (_capture != null) _capture.Dispose();

the app work fine but when i close the app throw me: Message: Context0x23754b0' Disconnected. ... how to fix this problem?


I assume this would have something to do with your camera capture library and how it potentially uses unmanaged resources.

I'd start by commenting all the code out of your RunWorkerCompleted to see if the message still happens. If it doesn't, then it's caused by one or more of the GetCaptureProperty calls. I suspect it won't though.

I see in the documentation of Egmu.CV.Capture that there is a Capture.DisposeObject() method that talks about releasing the captured object. My guess is that after you instantiate _capture and you do what you need to do, you have to do a clean-up. I'd suggest that after your ProcessFrame finishes (or on exit of your application) that you try calling _capture.DisposeObject() to see if that cleans up and exits gracefully.


If all else fails, the approach I would suggest is comment out as much of your code as you can to get to the point where you can exit the program without it throwing an Exception. Then, comment in parts of code until you can locate exactly what gets created or run that will eventually cause your exception on exit. Once you can localize that, you'll have a better idea how to fix it.

This is a COM related error, it no doubt happens because you create the Capture object on the background thread. A COM object has thread affinity, once the thread that creates it stops running, the COM object is dead and cannot be used anymore. Trying to use it anyway produces the warning.

That this doesn't occur in the RunWorkerCompleted event handler is quite remarkable, this must be buried inside the OpenCV or Emgu plumbing in a non-obvious way. That certainly doesn't mean it couldn't occur some day. You'll need to re-think this, it doesn't make much sense to only create the object on the worker and have everything else run on the UI thread. Do everything on the worker, including the disposing. Or none of it.

Need Your Help

nhibernate multicriteria CTE subquery

sql-server nhibernate criteria-api

I currently have a MultiCriteria query, which filters the results based on the ids being within a sub query

How should I append both an anchor and image tag to a container?

javascript jquery

I have the following code but I now need to have the image tag wrapped in an anchor tag with attributes. I would like to do this without using jQuery's .html() method.