c# - System.Reflection.MethodInfo.Invoke and multiple threads ( with return type ) -
i have been looking through other questions on site , have found post.
ben voigts answer useful , believe have working in system.
the issue have in cases need value returned method invocation.
i going leave comment on post rep isnt high enough leave comments.
hopefully either ben see post or else able extend answer include how return value.
please let me know if there other information require.
kind regards
ash
you have 2 options. either call methodinfo.invoke synchronously , wait result. or set callback method called once invocation complete. extending example linked to:
public void invokeonnewthread(methodinfo mi, object target, action<object> callback, params object[] parameters) { threadstart threadmain = delegate () { var res = mi.invoke(target, parameters); if(callback != null) callback(res); }; new system.threading.thread(threadmain).start(); }
i added parameter takes delegate called when invocation done. can use way:
void main() { var test = new test(); var mi = test.gettype().getmethod("hello"); invokeonnewthread(mi, test, getresult); thread.sleep(1000); } public void getresult(object obj) { console.writeline(obj); }
Comments
Post a Comment