G
Guest
hi,
i have a program that used reflection to execute methods. now i want to
execute the reflected method on a new thread but cant figure out how or if it
can be done. take the below code, for instance.
// Declare the types.
string AssemblyToRun = "C:\MyAss";
string ClassToRun = "MyClass"
string MethodToRun = "MyMethod"
// Setup reflection.
Assembly MyAssemblyToRun = Assembly.LoadFrom(@AssemblyToRun + ".dll");
Type MyClassToRun = MyAssemblyToRun.GetType(AssemblyToRun + "." +
ClassToRun, true, true);
MethodInfo MyMethodToRun = MyClassToRun.GetMethod(MethodToRun);
// Create an instance of the class.
object MyClassInstance = Activator.CreateInstance(MyClassToRun);
// Execute the method.
object RetVal = MyMethodToRun.Invoke(MyClassInstance, BindingFlags.Default,
null, null, null);
simple right. ok. all the above works fine. now if i want to execute the
method on a new thread I would add something like:
Thread MyNewThread = new Thread(new ThreadStart(ClassToRun + "."
MethodToRun));
MyNewThread.Start();
that does not work because "ThreadStart" is a delegate that wants a "void"
method and all i can pass it is a class instance or the method name as a
string. it also wont work when i use the class instance or the MyMethodToRun
instance of the reflected method.
how can i use reflection and multithreading together? is this possibble? is
there a way to invoke a reflected method on a new thread?
HELP !!!
i have a program that used reflection to execute methods. now i want to
execute the reflected method on a new thread but cant figure out how or if it
can be done. take the below code, for instance.
// Declare the types.
string AssemblyToRun = "C:\MyAss";
string ClassToRun = "MyClass"
string MethodToRun = "MyMethod"
// Setup reflection.
Assembly MyAssemblyToRun = Assembly.LoadFrom(@AssemblyToRun + ".dll");
Type MyClassToRun = MyAssemblyToRun.GetType(AssemblyToRun + "." +
ClassToRun, true, true);
MethodInfo MyMethodToRun = MyClassToRun.GetMethod(MethodToRun);
// Create an instance of the class.
object MyClassInstance = Activator.CreateInstance(MyClassToRun);
// Execute the method.
object RetVal = MyMethodToRun.Invoke(MyClassInstance, BindingFlags.Default,
null, null, null);
simple right. ok. all the above works fine. now if i want to execute the
method on a new thread I would add something like:
Thread MyNewThread = new Thread(new ThreadStart(ClassToRun + "."
MethodToRun));
MyNewThread.Start();
that does not work because "ThreadStart" is a delegate that wants a "void"
method and all i can pass it is a class instance or the method name as a
string. it also wont work when i use the class instance or the MyMethodToRun
instance of the reflected method.
how can i use reflection and multithreading together? is this possibble? is
there a way to invoke a reflected method on a new thread?
HELP !!!