How can I properly unsubscribe to an event and be sure that the called method is not called now ?
My problem is with this kind of code :
public class MyClassWithEvent
{
public event EventHandler MyEvent;
public int Field;
}
public class MyMainClass
{
private MyClassWithEvent myClass;
public void Start()
{
myClass.MyEvent += new EventHandler(doSomething);
}
public void Stop()
{
myClass.MyEvent -= new EventHandler(doSomething);
myClass = null;
}
private void doSomething()
{
myClass.Field = 42;
}
}
If myClass = null
is called while doSomething
is executing, instruction myClass.Field = 42
raise an error because myClass is null.
How can I be sure that doSomething
is not executing before setting myClass = null
?
Edit:
Other example:
public void Stop()
{
myClass.MyEvent -= new EventHandler(doSomething);
// Can I add a function here to be sure that doSomething is not running ?
myClass.Field = 101;
}
In that case, I will not be sure if myClass.Field
is 42 or 101.
Edit2:
Apparently my question is not as simple as I thought. I will explain my precise case.
My code is :
public class MyMainClass
{
object camera;//can be type uEye.Camera or DirectShowCamera
bool isRunning = false;
public void Start()
{
if (camera is uEye.Camera)
{
camera.EventFrame += new EventHandler(NewFrameArrived);
}
else if (camera is DirectShowCamera)
{
//other init
}
isRunning = true;
}
public void Stop()
{
if (camera is uEye.Camera)
{
camera.EventFrame -= new EventHandler(NewFrameArrived);
camera.exit;
}
else if (camera is DirectShowCamera)
{
//other stop
}
isRunning = false;
}
public void ChangeCamera(object new camera)
{
if (isRunning)
Stop()
camera = new camera();
}
void NewFrameArrived(object sender, EventArgs e)
{
uEye.Camera Camera = sender as uEye.Camera;
Int32 s32MemID;
Camera.Memory.GetActive(out s32MemID);
lock (_frameCameralocker)
{
if (_frameCamera != null)
_frameCamera.Dispose();
_frameCamera = null;
Camera.Memory.ToBitmap(s32MemID, out _frameCamera);
}
Dispatcher.Invoke(new Action(() =>
{
lock (_frameCameralocker)
{
var bitmapData = _frameCamera.LockBits(
new System.Drawing.Rectangle(0, 0, _frameCamera.Width, _frameCamera.Height),
System.Drawing.Imaging.ImageLockMode.ReadOnly, _frameCamera.PixelFormat);
if (_frameCamera.PixelFormat == System.Drawing.Imaging.PixelFormat.Format8bppIndexed)
{
DeviceSource = System.Windows.Media.Imaging.BitmapSource.Create(
bitmapData.Width, bitmapData.Height, 96, 96, System.Windows.Media.PixelFormats.Gray8, null,
bitmapData.Scan0, bitmapData.Stride * bitmapData.Height, bitmapData.Stride);
}
_frameCamera.UnlockBits(bitmapData);
if (OnNewBitmapReady != null)
OnNewBitmapReady(this, null);
}
}));
}
}
And when I change the camera from uEye to directshow sometime I have a AccessViolationException in DeviceSource = System.Windows.Media.Imaging.BitmapSource.Create
(method NewFrameArrived
) because I try to create BitmapSource from an exited camera