c# - Error when starting a new thread and opening dialog using Mono API -


i'm working on native dll written in c++ uses mono show graphical user interface. i've written simple skeleton, works error under conditions.

first here c# code i'm calling mono api

using system; using system.collections.generic; using system.windows.forms; using system.threading;  namespace testapp {     static class program     {         // creates new thread , runs dialog() on it,          // opens dialog window         static void start()         {             console.writeline("running thread in mono");             thread t = new thread(new threadstart(dialog));             t.start();         }          public static void dialog()         {             form f = new form();             f.showdialog();             f.dispose();         }     } } 

this gets compiled testapp.dll

in c/c++ code following:

  1. load assembly
  2. locate start() method , run it
  3. enter endless loop reads input console
  4. when input string "open" received, run start() again

now, @ beginning, form opens, works (doesn't freeze on screen it's running in own thread) , can open more instances of form typing "open" @ prompt. exception thrown when close open forms , attempt open new 1 (typing "open" again after closing opened forms).

unhandled exception: system.outofmemoryexception: not enough memory complete operation [gdi+ status: outofmemory]   @ system.drawing.gdiplus.checkstatus (status status) [0x00000] in <filename unknown>:0   @ system.drawing.graphics.fromhwnd (intptr hwnd) [0x00000] in <filename unknown>:0   @ system.windows.forms.xplatuiwin32.getautoscalesize (system.drawing.font font) [0x00000] in <filename unknown>:0   @ system.windows.forms.xplatui.getautoscalesize (system.drawing.font font) [0x00000] in <filename unknown>:0   @ system.windows.forms.form.getautoscalesize (system.drawing.font font) [0x00000] in <filename unknown>:0   @ system.windows.forms.form..ctor () [0x00000] in <filename unknown>:0   @ (wrapper remoting-invoke-with-check) system.windows.forms.form:.ctor ()   @ testapp.program.dialog () [0x00000] in <filename unknown>:0   @ system.threading.thread.startunsafe () [0x00000] in <filename unknown>:0 

can me decrypt message? :)

somehow, when close last form window, guess mono decides unload/shut down critical component prevents me opening window after point in time.

here c++ (well, c actually) code use:

#define _crt_secure_no_warnings  #include <mono/jit/jit.h> #include <mono/metadata/object.h> #include <mono/metadata/environment.h> #include <mono/metadata/assembly.h> #include <mono/metadata/debug-helpers.h> #include <stdlib.h> #include <stdio.h> #include <string.h>  static void runthread(monodomain* domain, monoassembly* assembly) {     monoimage* image = mono_assembly_get_image (assembly);      monoclass *klass;     monoobject *obj;     monomethod *m = null, *start = null;     void* iter = null;     klass = mono_class_from_name (image, "testapp", "program");      // find method start()     while ((m = mono_class_get_methods (klass, &iter)))      {         if (strcmp (mono_method_get_name (m), "start") == 0)          {             start = m;             break;         }     }      mono_runtime_invoke (start, null, null, null); }  int main(int argc, char* argv[]) {     monodomain *domain;     const char *file;     int retval;      if (argc < 2)     {         fprintf (stderr, "please provide assembly load\n");         return 1;     }      file = argv [1];     domain = mono_jit_init (file);     monoassembly *assembly;     assembly = mono_domain_assembly_open (domain, file);      if (!assembly)     {         printf("can not load assembly");         exit (2);     }      // open dialog     runthread(domain, assembly);      // endless loop     char *p = new char[100];     while(1)     {         gets (p);          // open dialog         if( strcmp(p, "open") == 0)             runthread(domain, assembly);     }      retval = mono_environment_exitcode_get ();     mono_jit_cleanup (domain);     return retval; } 

after researching lot more can safely bug in mono. i've submitted bug report on matter.

this not problem regarding c api able recreate bug in c# code. when opening form first time on new thread mono seems initialization work. if thread stops running or runs out, reference of whatever lost, , calls open forms or dialogs after fail. unexpected , unwanted behavior.


Comments