Sign in to follow this  
Headkaze

Strange KeyUp event problem

Recommended Posts

Headkaze    607
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.

Here is some code.

[code]private Object m_thisLock = new Object();

private void DisposeLayoutObjects()
{
lock (m_thisLock)
{
Console.WriteLine(String.Format("DisposeLayoutObjects Start (ThreadId:{0})", Thread.CurrentThread.ManagedThreadId));

for (int i = 0; i < MainForm.Layout.ObjectArray.Count; i++)
MainForm.Layout.ObjectArray[i].Dispose();

MainForm.Layout.ObjectArray.Clear();

Console.WriteLine(String.Format("DisposeLayoutObjects End (ThreadId: {0})", Thread.CurrentThread.ManagedThreadId));
}
}
private void LoadLayout()
{
DisposeLayoutObjects()
...
}
protected override void OnKeyUp(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Down:
LoadLayout();
break;
}
}
[/code]

I am getting the following output when I tap on the Down key fast.

[code]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)[/code]

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.

Share this post


Link to post
Share on other sites
Headkaze    607
No I'm not registering any other event.

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.

I have no idea how the dll is causing this.

Share this post


Link to post
Share on other sites
Headkaze    607
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:

[code]public void DisposeLayoutObjects()
{
lock (m_thisLock)
{
Console.WriteLine(String.Format("DisposeLayoutObjects Start (ThreadId:{0})", Thread.CurrentThread.ManagedThreadId));

foreach (LayoutObject layoutObject in MainForm.Layout.ObjectArray)
layoutObject.Dispose();

MainForm.Layout.ObjectArray.Clear();

Console.WriteLine(String.Format("DisposeLayoutObjects End (ThreadId: {0})", Thread.CurrentThread.ManagedThreadId));
}
}[/code]

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".

Share this post


Link to post
Share on other sites
Headkaze    607
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;

if (!pFilterGraph)
return E_POINTER;

hr = pFilterGraph->EnumFilters(&pEnumFilters);

if (FAILED(hr))
return hr;

pEnumFilters->Reset();

while(pEnumFilters->Next(1, &pFilter, &cFetched) == S_OK)
{
hr = E_FAIL;

IPin *pPin = NULL, *pPinConnected = NULL;
IEnumPins *pEnumPins = NULL;

pFilter->EnumPins(&pEnumPins);
pEnumPins->Reset();

while (pEnumPins->Next(1, &pPin, NULL) == S_OK)
{
BOOL bMatch = FALSE;
hr = IsPinDirection(pPin, PINDIR_OUTPUT, &bMatch);

if(!bMatch)
{
SAFE_RELEASE(pPin);
continue;
}

if (pPin->ConnectedTo(&pPinConnected) == VFW_E_NOT_CONNECTED)
{
CComPtr <IEnumMediaTypes> piMediaTypes;

if (SUCCEEDED(hr = pPin->EnumMediaTypes(&piMediaTypes)))
{
piMediaTypes->Reset();

AM_MEDIA_TYPE *mediaType;

while (piMediaTypes->Next(1, &mediaType, 0) == NOERROR)
{
if (mediaType->majortype == majortype)
{
hr = pFilterGraph->RenderEx(pPin, connectExisting ? AM_RENDEREX_RENDERTOEXISTINGRENDERERS : 0, NULL);
}

DeleteMediaType(mediaType);
}
}
}

SAFE_RELEASE(pPin);
SAFE_RELEASE(pPinConnected);
}

pEnumPins->Release();
pFilter->Release();
}

pEnumFilters->Release();

return S_OK;
}[/source]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this