新线程中的动态方法调用

时间:2011-01-02 06:51:23

标签: c# multithreading reflection dynamic

有些人能帮我一把吗?我需要这个方法根据用户从主窗体中的选择在新线程中调用用户所需的方法。我认为我很接近,但我坚持如何传递一个变量代表用户在新线程中选择方法的开始。

继承我的代码

    public void GetTest()
    {
        _t = testListBox.SelectedIndex;

        if (_t < 0)
            return;

        _v = testListBox.SelectedValue;

        method = _v as MethodInfo;


        if (method == null)
            return;

        _selectedMethod = method.Name;

        MessageBox.Show(_selectedMethod.ToString());

        counter++;

        Thread UIthread = new Thread(new ThreadStart(??????)); // this is will be a method based on the user's choice in the main form thread
        // adding Name of new Thread
        UIthread.Name = "UIThread";
        UIthread.Start();
        // Update test status
        _testStatus = "Running";
        //Make thread global
        _UIthread = UIthread;
    }

3 个答案:

答案 0 :(得分:1)

这样的事情怎么样?

public partial class MainWindow : Window
{

    public MainWindow()
    {
        InitializeComponent();
        LoadList();
    }

    private void LoadList()
    {
        lstTest.Items.Clear();
        var t1 = new MethodInfo
                     {
                         MethodName = "Test1", 
                         MethodToRun = this.Test1
                     };
        lstTest.Items.Add(t1);


        var t2 = new MethodInfo
                    {
                        MethodName = "Test2",
                        MethodToRun = this.Test2
                    };
        lstTest.Items.Add(t2);
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        var t = lstTest.SelectedItem as MethodInfo;
        if (t != null)
        {
            var a = new Task(t.MethodToRun);
            a.Start();
        }
    }

    private void Test1()
    {
        MessageBox.Show("Test1 started");
    }

    private void Test2()
    {
        MessageBox.Show("Test2 started");
    }

}

public class MethodInfo
{
    public string MethodName;
    public Action MethodToRun;

    public override string ToString()
    {
        return MethodName;
    }

}

答案 1 :(得分:0)

您可以在新线程上执行lamdba表达式。

我不确定你是在处理静态方法还是实例方法。如果您的方法是静态的,那么您不需要创建对象实例:

method = _v as MethodInfo;
object[] parameters = null;

Thread UIthread = new Thread(() =>
{
    object obj = null;

    if (!method.IsStatic)
    {
        obj = Activator.CreateInstance(method.DeclaringType);
    }

    method.Invoke(obj, parameters);
});

UIthread.Start();

答案 2 :(得分:0)

感谢所有人的帮助。这是所有关心的人的答案。您创建一个线程启动委托。我在阅读了这两篇文章后发现了这一点:http://bytes.com/topic/net/answers/410294-using-reflection-multithreading http://www.pelennorfields.com/matt/2009/03/13/createdelegate-error-binding-to-target-method/

此方法采用反映的MethodInfo GetMethod列表框值,并在新线程中调用相应的Instance方法。该线程是全局的,允许用户控制长时间运行的方法。该方法很棒,因为它允许动态类的实例方法,而无需重新编译整个program.code

        public void GetTest()
    {
        _t = testListBox.SelectedIndex;

        if (_t < 0)
            return;

        _v = testListBox.SelectedValue;

        method = _v as MethodInfo;


        if (method == null)
            return;

        _selectedMethod = method.Name;

        MessageBox.Show(_selectedMethod.ToString());

        counter++;


        ThreadStart ts = (ThreadStart)Delegate.CreateDelegate(typeof(ThreadStart), this, method, true);

        Thread UIthread = new Thread(ts);
        UIthread.Start();
        _UIthread = UIthread; 
    }