.NET Framework - Control MarshalingControl

Asked By Armin Zingler on 07-Apr-11 09:47 AM

as you do not like long descriptions: How can I avoid or control the
creation of a MarshalingControl?



Jason Keats replied to Armin Zingler on 07-Apr-11 11:14 PM
As I do not like long answers:
Armin Zingler replied to Jason Keats on 08-Apr-11 10:25 AM
Am 08.04.2011 05:14, schrieb Jason Keats:

That's what I can expect from my question. :) I thank you, though
I am trying to elaborate a bit more:
My problem is that I got a dead-lock in my application running
two UI-threads independently. That means, I have payed great attention
to not creating a dead-lock in my own code. If you look at it, you would
also be surprised that it can happen.
To be more specific, the line "New MyForm" does not return.
I do not do anything special in it. The window handle of the
Form is also not created inside because I do know that it
could cause a dead-lock. That's why *I* avoided it. However,
*the FW* internally creates a MarshallingControl and it is handle
inside "New MyForm". I am a bit lost how to avoid this.

Tom Shelton replied to Armin Zingler on 08-Apr-11 02:30 PM
Armin Zingler laid this down on his screen :

Armin - I am having a bit of trouble following what your trying to
accomplish here...  There should not be an issue just creating forms on
different threads - is there isome whay you could create a smallish
example that demonstrates your problem?

Tom Shelton
Armin Zingler replied to Tom Shelton on 08-Apr-11 04:16 PM
Am 08.04.2011 20:30, schrieb Tom Shelton:

Thanks Tom for asking. Smallish is difficult. But I try to describe:
I wrote a TraceListener. It starts a new thread. The thread creates
a Form to display the output of the TraceListener. The TraceListener
must wait a short moment until the Form has been created:

f_Form = New Traceform


WaitOne must be used carefully. I myself did not put any blocking code into
a Dll and have used it from several projects (successfully).

In my current project, I am using the library, too. I put Debug.Writeline into
the WndProc of my startup Form. Result: Freeze at start-up. I found out that
the main thread is stuck in

CreateHandle -> WndProc -> Debug.Writeline -> TraceListener -> FormCreatedEvent.WaitOne

I wondered why WaitOne still waits because "New TraceForm" in the other thread
should be done in a millisecond. So, why does "New TraceForm" not return? The
callstack shows:

New TraceForm -> .... -> New MarshalingControl -> MarshalingControl.Creathandle

Conclusion: While the main thread is inside CreateHandle waiting for the
Form in the other thread being created, that thread is also inside
CreateHandle trying to create the MarshalingControl. The FW synchronizes
simultaneous creation of window handles from different threads. That's ok,
and I do not do it myself, but due to that MarshalingControl which is out
of my own scope of responsibility, the dead-lock occurs.

Armin Zingler replied to Armin Zingler on 12-Apr-11 09:10 AM
Am 08.04.2011 22:16, schrieb Armin Zingler:

I kept it as comprehensive and short as possible. I am unable to do better.

Tom Shelton replied to Armin Zingler on 12-Apr-11 09:19 AM
Armin Zingler presented the following explanation :

Hmmmm...  I am not seeing an obvious answer atm.  I might have to play
with this a bit - if I get time.

Tom Shelton
Armin Zingler replied to Armin Zingler on 12-Apr-11 09:47 AM

project to reproduce:


Please note that some code has been copied and pasted
from several sources to make it work.

Jason Keats replied to Armin Zingler on 14-Apr-11 10:22 AM
I think you mean that you have copied and pasted the minimum code required
to show it does not work. :-)

it is still impressively complicated, Armin.

I put together a simpler version, that seems to work:

BTW, I still think retlang might provide a (better) solution. I just
have not had time to prove it.
Armin Zingler replied to Jason Keats on 14-Apr-11 11:48 AM
Am 14.04.2011 16:22, schrieb Jason Keats:

I meant: "...to make it /compile/."

Why? Just start it, break, and switch to the thread named "TraceForm".

Jason, thanks a lot for taking the time. However, I am talking about a
multi-threading issue. You do not start a new thread in your project,
so I do not see the relation to my problem.

I have not looked at it yet. Actually I am interested if I did anything wrong.
I am pretty sure I did not - though it does not work.

Jason Keats replied to Armin Zingler on 15-Apr-11 08:31 AM
I think that anything that uses the ".NET way" of threading is
complicated. Your problem proves it. That's why I suggested trying the
pub/sub method used by retlang. Unfortunately, it is a .NET 4.0 library.
Therefore, as I believe you are using VS2008, you are out of luck.

As I was not sure whether you were after a working program or just had a
theoretical interest in your particular error, I provided my little
experiment in trace listeners - in case it was useful. I am not surprised
that it was not. ;-)

I have found tracing helpful in ASP.NET development, but have not used it
with Windows Forms. I can see how a DLL that immediately displays
diagnostic information via a Form could be useful.

Good luck with finding a solution using your current methodology.
Armin Zingler replied to Jason Keats on 15-Apr-11 01:02 PM
Am 15.04.2011 14:31, schrieb Jason Keats:

Thanks anyway! I will report if I find a solution.

Armin Zingler replied to Armin Zingler on 16-Apr-11 07:22 AM
Am 15.04.2011 19:02, schrieb Armin Zingler:

One approach that I have thought of but wanted to avoid if there is
another solution is moving the message buffer from the Form to the
TraceListener itself. This way, the TraceListener does not have
to wait for the Form to be created.
I will do so...

Armin Zingler replied to Armin Zingler on 16-Apr-11 08:15 AM
Am 16.04.2011 13:22, schrieb Armin Zingler:

Did so. Works.