Dynamic method inovcation in new thread
Can some give me hand here? I need this method to invoke the user's desired method in a new thread based on the user's choice from the main form. I think i am close, but I am stuck at how to pass in a variable the represents that user's choice of method in the new thread start.开发者_运维百科
heres my 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++;
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;
}
How about something like this.
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;
}
}
You can execute a lamdba expression on your new thread.
I'm not sure if you are dealing with static or instance methods. If your methods are static then you don't need to create an object instance:
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();
Thanks to everyone for all the help. Here's the answer for all the folks that care. You Create a thread start delegate. I found this out after reading these two posts: http://bytes.com/topic/net/answers/410294-using-reflection-multithreading http://www.pelennorfields.com/matt/2009/03/13/createdelegate-error-binding-to-target-method/
This method takes a reflected MethodInfo GetMethod listbox value and invokes the corresponding Instance method in a new thread. The thread is made global to allow the user to control the long-running method. The method is great because it allows for a dynamic class of instance methods, with out recompiling the entire program.code here
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;
}
精彩评论