Dynamic Thread Creation

Greetings everyone,

I have found myself in an empasse while trying to dynamically create an array of threads. Let me try to explain what I mean by "dinamically creating".

One of the four overloads of the Thread constructor takes as a parameter a ThreadStart delegate, which needs a method name in order to be fulfilled:

System.Threading.Thread m_MyThread = new Thread(new ThreadStart(MyMethod));

My scenario is different and a bit more complicated, in fact I haven't been able to find a working solution - I'm not even sure if what I'm trying to accomplish it's possible at all.
I am developing a plug-in system for a commercial 3D engine. Some methods inside these plugins can be run on a different thread if the plugin developer so desires.

// the float value is the "update delay" value.

[UseMethodInSeparateThread(0.33f)]
// Defined in the IPlugIn interface
void Update()
{
...
}


For example, the plugin developer is creating a sky renderer, and the maths behind colour and stars position calculations etc are going to be in a separate thread.

By using Reflection, I am able to collect informations about the methods' attributes, thus allowing me to know whetever a method has this "UseMethodInSeparateThreadAttribute" attribute.

foreach (Attribute m_Attribute in Attribute.GetCustomAttributes(m_MethodInfo))
{
if (m_Attribute.GetType() == typeof(MyApp.Attributes.PlugInMethodInSeparateThreadAttribute))
{
m_UsesThread = true;
m_UpdateDelay = ((MyApp.Attributes.PlugInMethodInSeparateThreadAttribute)m_Attribute).UpdateDelay;
break;
}
}

if (m_UsesThread)
{
// Create a thread for this method in an existing Thread array.
}

My question is: how is it possible to create a Thread by collecting informations from a MethodInfo structure (or in a similar way for that matters) Or am I forced to know the method which I'm going to create a Thread for beforehand ("Method name expected.")

I hope I've explained this clearly.

Thanks in advance for your time,
r.

PS: As someone might have noticed, I have originally posted this thread in the wrong forum section. Apologies to the moderators.


Answer this question

Dynamic Thread Creation

  • xplosiv_1

    The Thread constructor requires a delegate, so you could try something like this:

       Delegate del = Delegate.CreateDelegate(typeof(plugin), methodInfo);
       Thread thread = new Thread(new ThreadStart(del));

    However, I can't help thinking you really should be using your IPlugin interface rather than reflection. eg.

       IPlugin plugIn = GetPlugin();
       Thread thread = new Thread(plugIn.Update);
       thread.Start();


  • Mathieu Cupryk

    Sorry, just checked my first bit of code and realised it doesn't compile!  So my next thought is to create a wrapper for the thread:


    public class ThreadWrapper
    {
        IPlugin plugin;
        MethodInfo method;

        public ThreadWrapper(IPlugin plugin, MethodInfo method)
        {
            this.plugin = plugin;
            this.method = method;
        }

        public void DoWork()
        {
            method.Invoke(plugin, null);
        }
    }

    IPlugin plugIn = GetPlugin();
    ThreadWrapper wrapper = new ThreadWrapper(plugIn,methodInfo);
    Thread thread = new Thread(wrapper.DoWork);
    thread.Start();

     


  • robinjam

    Hello Paul,

    Thanks for the reply. I'll check your solutions in my project. I believe the Delegate.CreateDelegate function is what I am looking for.

    I cannot use the second method you posted, because I don't know which Interface method is going to be set with the UseInSeparateThreadAttribute - thus the need of creating a delegate dynamically. It could have been the "Render" method, for instance.
    A solution would be testing the method name against a switch block, but since I have extensibility in mind, I'd prefer not to have built-in strings for the Interface definition.

    i.e.: (pseudocode)
    switch (MethodName)
    case "Render" : thread = new Thread(plugin.Render);
    case "Update" : thread = new Thread(plugin.Update);

    and so on.


    I'll be posting my results as soon as possible, thanks again.

  • Alain DePreter

    Yep avoid switch's at all costs! :o)

     


  • gb05

    Hello Paul,

    many thanks for your useful posts, my plug-in system is finally working as intended.

    Cheers :),
    Roberto


  • Dynamic Thread Creation