线程调用有关问题
线程调用问题?
简而言之,就是我想在Class B中创建一个线程,去执行Class A中的proc方法。
但是上面的代码不行,怎么办?
------解决方案--------------------
既然你已经传过来类的实例了,就直接_className.xxx就行了,方法必须是public的,如果通过反射可以这样
- VB.NET code
Class A Sub proc() End Sub End Class Class B Private Sub xxxx(ByRef _className As Object, ByVal _procName As String) Dim t As New Thread(AddressOf _className.GetType.GetMethod(_procName)) '错误 “AddressOf”操作数必须是某个方法的名称(不带圆括号)。 End Sub End Class
简而言之,就是我想在Class B中创建一个线程,去执行Class A中的proc方法。
但是上面的代码不行,怎么办?
------解决方案--------------------
既然你已经传过来类的实例了,就直接_className.xxx就行了,方法必须是public的,如果通过反射可以这样
- C# code
public class MyClass { public void ShowNum() { for(int i=0;i<100;i++) { Console.WriteLine(i.ToString()); } } } private void FOO() { MethodInfo method = typeof(MyClass).GetMethod("ShowNum"); if (method != null) { method.Invoke(new MyClass(), null); } } private void button10_Click(object sender, EventArgs e) { MethodInfo method = typeof(MyClass).GetMethod("ShowNum"); Thread thread = new Thread(new ThreadStart(FOO)); thread.Start(); }
------解决方案--------------------