I am writing an engine using SlimDX and as part of its development I am writing an editor so I can easily test things.
So I import the engine as a dll into a standard forms application and then place the SlimDX window inside a scrollable panel.
As part of my testing I am using a KeyUp event to load some textures and display them on some quads. But something strange is happening if I tap the key very fast. It appears to be entering a method twice before it exits like it's being called from another thread.
I am getting the following output when I tap on the Down key fast.
DisposeLayoutObjects Start (ThreadId:9)
The thread 0xfb8 has exited with code 0 (0x0).
DisposeLayoutObjects Start (ThreadId:9)
DisposeLayoutObjects End (ThreadId: 9)
DisposeLayoutObjects End (ThreadId: 9)
The thread id is the same, I even added a lock to prevent re-entry. So how is it possible that it's entering DisposeLayoutObjects twice like that?
As a side note my main loop is using Application_Idle event with a PeekMessage for pumping messages. When I tried using a timer I had some UI refresh issues.
I should have mentioned this earlier but part of the LoadLayout method involves loading a C++/CLI COM dll that uses DirectShow to render video to a texture. It turns out if I don't use this dll the issue does not appear.
I didn't mention it earlier because I didn't think a dll could cause events to go wierd like that but it looks like it can.
This is driving me insaine. I can not for the life of me figure out how or why DisposeLayoutObjects() can be executed at the same time on the same thread.
Out of interest sake just to prove this is happening if I change the code to the following:
foreach (LayoutObject layoutObject in MainForm.Layout.ObjectArray)
layoutObject.Dispose();
MainForm.Layout.ObjectArray.Clear();
Console.WriteLine(String.Format("DisposeLayoutObjects End (ThreadId: {0})", Thread.CurrentThread.ManagedThreadId));
}
}
Note now I'm using a foreach loop. Now if I press the Down key slowly it works as it should. If I tap the key twice quickly it will break the debugger in the foreach loop with an InvalidOperationException error stating the "Collection was modified; enumeration operation may not execute".
It turns out it was in my C++/CLI dll afterall. For some reason adding the IsPinDirection check to make sure the pin is PINDIR_OUTPUT fixed the problem. Very strange. If someone could explain to me why I'd very much appreciate it. It's been a long night tracking this issue down.
[source lang="cpp"]HRESULT RenderAllStreams(IFilterGraph2 *pFilterGraph, GUID majortype, BOOL connectExisting)
{
IEnumFilters *pEnumFilters = NULL;
IBaseFilter *pFilter = NULL;
ULONG cFetched;
HRESULT hr;