[Mono-list] Opening a form on a new thread, called from C API

valdiorn valdiorn at gmail.com
Mon May 2 22:16:05 EDT 2011


Hi. I'm trying to develop a C console program that can open a Mono Form when
I type in the command "open".

in main() I do something like this:

while(1)
{
	gets (p);

	// do some work, irrelevant to this question...

	if( strcmp(p, "open") == 0)
		mono_runtime_invoke (dialog, NULL, NULL, NULL);
}


dialog is the MonoMethod I want to run. It's defined in a C# dll assembly
that I have already loaded.

Now, the dialog function is simply :

public static void dialog()
{
	Form f = new Form();
	Application.Run(f);
}

This works pretty well. The form opens, but it runs on the same thread as
the main program, so the console freezes while the GUI is open. I want to
start a new thread and run dialog() from there.

I modify my C code to:

if( strcmp(p, "open") == 0)
		mono_runtime_invoke (startThread, NULL, NULL, NULL);

where startThread is another monoMethod. It is this method (in the loaded C#
assembly):

public static void startThread()
{
	Thread t = new Thread(new ThreadStart(dialog));
	t.Start();
}

Now the form starts on a new thread, and I can even open several forms if I
want (by typing "open" repeatedly at the prompt. I want this behavior, so
it's all good).

Everything is fine until I close ALL the forms and then try to launch a new
form again. When I do this the program crashes, giving me this error: 

Unhandled Exception: System.OutOfMemoryException: Not enough memory to
complete operation [GDI+ status: OutOfMemory]
  at System.Drawing.GDIPlus.CheckStatus (Status status) [0x00000] in
<filename unknown>:0
  at System.Drawing.Graphics.FromHwnd (IntPtr hwnd) [0x00000] in <filename
unknown>:0
  at System.Windows.Forms.XplatUIWin32.GetAutoScaleSize (System.Drawing.Font
font) [0x00000] in <filename unknown>:0
  at System.Windows.Forms.XplatUI.GetAutoScaleSize (System.Drawing.Font
font) [0x00000] in <filename unknown>:0
  at System.Windows.Forms.Form.GetAutoScaleSize (System.Drawing.Font font)
[0x00000] in <filename unknown>:0
  at System.Windows.Forms.Form..ctor () [0x00000] in <filename unknown>:0
  at (wrapper remoting-invoke-with-check) System.Windows.Forms.Form:.ctor ()
  at testApp.Program.dialog () [0x00000] in <filename unknown>:0
  at System.Threading.Thread.StartUnsafe () [0x00000] in <filename
unknown>:0

I can't understand why this happens. and remember, it only happens after I
close ALL the forms I had previously opened, and then try to open an new one
after that. As long as I leave at least one form open on the screen, it
doesn't crash.

I previously posted this question on stackOverflow. For full code see:

http://stackoverflow.com/questions/5838431/error-when-starting-a-new-thread-and-opening-dialog-using-mono-api

I did not get any answers there. hopefully you can help! :)

--
View this message in context: http://mono.1490590.n4.nabble.com/Opening-a-form-on-a-new-thread-called-from-C-API-tp3491548p3491548.html
Sent from the Mono - General mailing list archive at Nabble.com.


More information about the Mono-list mailing list