Hi i'm using Embedded Mono with NaCl. Everything is working so far but idk how to invoke a .NET method from c thats not on the same thread as the .NET exe was executed on.

Here is the method i'm trying to use:

void Mono_InvokeMethod(const char* assemblyName, const char* method, const char* arg)
{
    MonoAssemblyName* man = mono_assembly_name_new(assemblyName);
    assert(man);
    MonoAssembly* ma = mono_assembly_loaded(man);
    assert(ma);
    MonoImage* mi = mono_assembly_get_image(ma);
    assert(mi);
    MonoMethodDesc* mmd = mono_method_desc_new(method, "Second.Program" /* include_namespace */);
    assert(mmd);
    MonoMethod* mm = mono_method_desc_search_in_image(mmd, mi);
    assert(mm);
    void *args[1];
    args[0] = mono_string_new(mono_get_domain(), arg);//mono_get_root_domain() <<<<<<< Maybe use this instead

    mono_runtime_invoke(mm, NULL /* "this" object */, args, NULL /* exception */);
}
有帮助吗?

解决方案

You must start the new thread yourself and then call mono_thread_attach before mono_runtime_invoke. This is how it would look like, assuming thread_callback is called on a separate thread:

void thread_callback (MonoMethod *method, void **args)
{
    mono_thread_attach (mono_get_root_domain ());
    mono_runtime_invoke (method, NULL, args, NULL);
}

You can call mono_thread_attach as many times as you want, you don't have to keep track of whether you've called it for a particular thread or not.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top